473,466 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

2 New Member
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 9920
Banfa
9,065 Recognized Expert Moderator Expert
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 Contributor
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 New Member
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
1
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.