How to Get Character Position (Index) in a String in Python(Examples)
This tutorial explains how to determine the position of a character in a given string in the Python language.
A string is a sequence of characters enclosed in double quotes, and the characters in a string are indexed starting from 0.
For example, the starting character position is 0.
How to Get Character Position in a String in Python?
- Using the find() Function
The find()
function checks the given substring’s position and returns the position if a matching substring is found. It returns -1
if no match is found.
Syntax:
str.find(substring[, start[, end]])¶
Arguments:
substring
: Substring to search in the given string; returns the first occurrence of the string. start
and end
options are optional indices. For example, start=1 and end=4 consider characters from position 1 to position 3.
Returns:
- Index position if found.
-1
is returned if no matching character/substring is found in the given string.
This function is case-sensitive.
Here is an example
name="John"
print( name.find('o'))
print( name.find('J'))
print( name.find('j'))
Output:
1
0
-1
- Using index() function The
index()
function returns the position or index of a given character in a string.
Syntax:
string.index(charcter[, start[, end]])
Arguments:
character
: Character to search in the given string; returns the first occurrence of the character. start
and end
options are optional indices. For example, start=1 and end=4 consider characters from position 1 to position 3.
Returns:
- Index position if found.
- Throws a
ValueError
if no matching character is found in the given string.
This function is case-sensitive.
Here is an example
name="John"
print( name.index('o'))
print( name.index('J'))
print( name.index('j'))
1
0
Traceback (most recent call last):
File "a:\work\python\string.py", line 5, in <module>
print( name.index('j'))
ValueError: substring not found
Using a For Loop with the enumerated Function
This method involves finding a position using brute force search:
- Iterate through the string using the
enumerate
function. - Use a for loop with an index syntax to check each character and return the index if a matching character is found.
Here is an example
name="John"
for index, character in enumerate(name):
if "J"==character: {
print(index)
}
0
Conclusion
In this tutorial, you learned multiple ways to find the position of a character in a string:
- index() function
- find() function
- For loop with index syntax
The index()
function is recommended as it handles both matching and non-matching cases, returning the position of the character and performing well in terms of performance compared to the other two approaches.
The disadvantages of the other approaches are:
find()
method throws a ValueError if no match is found. For loop iterates over all characters in a string in the worst case, making it less advisable in terms of performance.
Learned multiple ways to find the position of a character in a string
- index() function
- find() function
- for loop with index syntax
The first approach is better, It checks for matching and nonmatching use cases and returns the position of a number, and good in performance compared with the other two approaches the disadvantages with the other are
- find() method throws ValueError if no match is found.
- For loop iterates all characters in a string in the worst case, so not advisable in terms of performance.