472,119 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

How do I get the position of a string inside a list in Python?

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.

Expand|Select|Wrap|Line Numbers
  1. sentence = list(input('Please type a sentence: '))
  2. space = ' '
  4. for space in sentence:
  5.     if space in sentence:
  6.         space_position = sentence[space]
  7.         print(space_position)
May 12 '21 #1
3 8172
9,065 Expert Mod 8TB
a. Don't convert the string to a list, that isn't helpful
b. Use either the find or index method on the string depending on how you wish to handle errors (https://docs.python.org/2/library/string.html)
c. Consider using raw_input instead of input so that you always have a string.
May 18 '21 #2
272 256MB
When searching for a character string using the "find" method or "index" method, there may be multiple search character strings, so it is necessary to search while shifting the start position in a loop.
In such cases, the "finditer" method is useful.
Expand|Select|Wrap|Line Numbers
  1. import re
  2. sentence = str(input("Please type a sentence:"))
  3. r = re.finditer("o",sentence)
  4. for e in r:
  5.     print (e.start())
May 19 '21 #3
25 16bit
You can use the index() method of a list to get the position of a string in it. Here's an example:
my_list = ['apple', 'banana', 'cherry', 'banana', 'date']
position = my_list.index('cherry')
print(position) # Output: 2
In the example above, index() method is used to find the position of the string "cherry" in the list my_list. The result is stored in the position variable, which is printed to the console.

If you're want to learn new technologies, join Cetpa Infotech.
Check out our website for more information.
1 Week Ago #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by olaufr | last post: by
12 posts views Thread by Anoop | last post: by
4 posts views Thread by Michael Yanowitz | last post: by
1 post views Thread by beginner | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.