Python Forum

Full Version: Very Basic String Manipulation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I'm new to Python and I'm trying to do very basic string manipulation, but I'm a bit stuck.
Method:
1) I input a string, say, "It is my world." ("I understand how to do this")
2) The program searches if any word(s) in the above string is found in a pre-defined list of words ("this too")
Note: For every word in the list, there is an equivalent/synonym ("This I have no idea")
For example, L = ['world' = 'universe']

3) If it is, insert the synonym in place of the original ("no idea")
4) print the result. ("this is straightforward")
5) If not ("this too")
print input()

If you could point me in the right direction, Much appreciated.
If I understood your requirements correctly, you can use a dictionary data container to store words (as keys) and their equivalents (as values). Find more on dictionaries here.
For replacing a substring with another, you can use string's replace method, see more here.
Thank you j.crater.