473,395 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 = ' '
  3.  
  4. for space in sentence:
  5.     if space in sentence:
  6.         space_position = sentence[space]
  7.         print(space_position)
May 12 '21 #1

✓ answered by SioSio

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())

3 9893
Banfa
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
SioSio
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
Vanisha
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.
Mar 17 '23 #4

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

Similar topics

5
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
3
by: **Developer** | last post by:
I need to know when the mouse is over the text of a TreeView node. I know how to get the Bounds of the node which appear to be the correct values to delineate the text. I could check the mouse...
12
by: Anoop | last post by:
Hi All Can any one help me out with the various depricated string functions that is followed in Python. For example how will be string.lower depricated. As far as string.lower('PYTHON') is...
21
by: Michael | last post by:
Hi All, I've received (via UDP) a null terminated string and need to convert it into a Python string. Can anyone tell me how this is done? If it helps, I know the number of characters in the...
2
by: yinglcs | last post by:
How to modify the source of a python file inside a python egg file? I can see the file by unzipping it, but how can I package it back as a python egg file after my modification. Thank you.
4
by: Michael Yanowitz | last post by:
Hello: If I have a long string (such as a Python file). I search for a sub-string in that string and find it. Is there a way to determine if that found sub-string is inside single-quotes or...
1
by: beginner | last post by:
Hi All, I am wondering if there is any way to handle exceptions inside list comprehension. For example, I want to skip the point if f(x) raises an exception. How can I do that without...
5
by: jc | last post by:
It would appear I cannot do this unless it's a richtextbox... but is there any way I can change the color of a given string inside a textbox control? Maybe with javascript? Postrender? CSS? ...
2
by: Jimmy45 | last post by:
Hello, I am trying to convert a string into list of strings. The output is like the following: , , ]. However, the output must be like: My code is like the following:
2
by: ifclick | last post by:
I want to add a new string inside a cell without deleting its previous value. The goal is one asset is part of multiple kits like a resistor. So i need a cell inside resistor asset with multiple...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.