Python Forum
Very Basic String Manipulation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Very Basic String Manipulation
#1
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.
Reply
#2
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.
Reply
#3
Thank you j.crater.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  trying to understand a string literal in a basic server program CompleteNewb 4 2,044 Nov-14-2021, 05:33 AM
Last Post: deanhystad
  IP string manipulation problem TheRealNoob 12 7,136 Feb-04-2019, 09:29 AM
Last Post: perfringo
  string manipulation Avarage_Joe 3 2,582 Sep-19-2018, 09:56 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020