I am trying to get the position of a string inside a list in Python? How should I do it?
For example, when a user provides a sentence "Hello my name is Dolfinwu", I turn the entire sentence into a list beforehand, and I want to get the positions of each "o" right here, how can I do it? In this case, the position of the first "o" is "4", and the position of the second "o" is "18". But obviously, users would enter different sentences by using different words, so how can I get a specific string value's position under this unpredictable situation?
I have tried this code as below. I know it contains syntax errors, but I could not figure out something better.
sentence = input('Please type a sentence: ')
space = ' '
for space in sentence:
if space in sentence:
space_position = sentence[space]
print(space_position)