Python Forum
Very Basic String Manipulation - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Very Basic String Manipulation (/thread-9017.html)



Very Basic String Manipulation - gstu - Mar-17-2018

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.


RE: Very Basic String Manipulation - j.crater - Mar-17-2018

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.


RE: Very Basic String Manipulation - gstu - Mar-18-2018

Thank you j.crater.