How can I get the position of a character inside a string in Python? Show
bad_coder 9,22119 gold badges37 silver badges61 bronze badges asked Feb 19, 2010 at 6:32
0 There are two string methods for this, Using find()
Using index()
From the Python manual
And:
Tomerikoo 16.6k15 gold badges37 silver badges54 bronze badges answered Feb 19, 2010 at 6:35
Eli BenderskyEli Bendersky 252k87 gold badges344 silver badges406 bronze badges 1 Just for a sake of completeness, if you need to find all positions of a character in a string, you can do the following:
which will print:
Jolbas 7375 silver badges15 bronze badges answered Sep 26, 2015 at 7:59
Salvador DaliSalvador Dali 204k142 gold badges684 silver badges745 bronze badges 4
"Long winded" way
to get substring,
answered Feb 19, 2010 at 6:36
ghostdog74ghostdog74 313k55 gold badges252 silver badges339 bronze badges 4 Just for completion, in the case I want to find the extension in a file name in order to check it, I need to find the last '.', in this case use rfind:
in my case, I use the following, which works whatever the complete file name is:
answered Sep 28, 2017 at 6:37
A.JolyA.Joly 2,1072 gold badges17 silver badges23 bronze badges 1 What happens when the string contains a duplicate character? from my experience with For example:
would return:
In that case you can do something like that:
answered Jul 1, 2015 at 12:40
DimSarakDimSarak 4422 gold badges5 silver badges11 bronze badges 1
Perhaps you'd like to have a look at the documentation to find out what the difference between the two is.
Brad Koch 18.2k18 gold badges107 silver badges135 bronze badges answered Feb 19, 2010 at 6:37
John MachinJohn Machin 79.4k11 gold badges138 silver badges183 bronze badges 1 A character might appear multiple times in a string. For example in a string
answered Sep 16, 2018 at 9:33
itssubasitssubas 1622 silver badges11 bronze badges If you want to find the first match.Python has a in-built string method that does the work: index().
Where:
If you want to find all the matches.Let's say you need all the indexes where the character The pythonic way would be to use
Or even better with a list comprehension:
answered Jan 26, 2021 at 5:01
Guzman OjeroGuzman Ojero 2,04018 silver badges18 bronze badges
Here we find
all index locations of the letter Given
Code
answered Feb 9, 2018 at 0:46
pylangpylang 36.2k11 gold badges120 silver badges110 bronze badges A solution with numpy for quick access to all indexes:
answered Jan 15, 2020 at 20:40
SebSeb 3024 silver badges6 bronze badges 2 Most methods I found refer to finding the first substring in a string. To find all the substrings, you need to work around. For example: Define the stringvars = 'iloveyoutosimidaandilikeyou' Define the substring
Define a function that can find the location for all the substrings within the string
Emi OB 2,4133 gold badges10 silver badges25 bronze badges answered Nov 5, 2021 at 8:44
How do you find the position of the character in the string?The indexOf() method returns the position of the first occurrence of specified character(s) in a string. Tip: Use the lastIndexOf method to return the position of the last occurrence of specified character(s) in a string.
How do you find the position of a word in a string in Python?Python String find() method returns the lowest index or first occurrence of the substring if it is found in a given string. If it is not found, then it returns -1. Parameters: sub: Substring that needs to be searched in the given string.
How do you find the index value of a character in Python?The standard solution to find a character's position in a string is using the find() function. It returns the index of the first occurrence in the string, where the character is found.
How do you find the location of a substring in a string?The indexOf() method returns the position of the first occurrence of substring in string. The first position in the string is 0. If the indexOf() method does not find the substring in string, it will return -1.
|