Searching characters in given strings is one of the most common tasks while text analysis in Python. This article discusses how to find the position of a character in a string in python.
- Find the Position of a Character in a String Using for Loop
- Find the Rightmost Index of the Character in a String Using for Loop
- Find the Position of a Character in a String Using While Loop
- Find the Rightmost Index of the Character in a String Using While Loop
- Find the Position of a Character in a String Using the find() Method
- Find the Index of a Character in a String Using the index() Method
- Find the Rightmost Index of a String in Python Using the rfind() Method
- Conclusion
Find the Position of a Character in a String Using for Loop
To find the position of a character in a string, we will first find the length of the string using the len()
function. The len()
function takes a string as its input argument and returns the length of the string. We will store the length of the string in a variable strLen
.
After getting the length of the string, we will create a range object containing values from 0 to strLen-1
using the range()
function. The range()
function takes the variable strLen
as its input argument and returns a range object.
Next, we will use a python for loop and the range object to iterate through the string characters. While iteration, we will check if the current character is the character whose position we are looking for. For this, we will use the indexing operator. If the current character is the character we are looking for, we will print the position of the character and exit the loop using the break statement in python. Otherwise, we will move to the next character.
After execution of the for loop, if the character is present in the input string, we will get the position of the character as shown below.
myStr="Python For Beginners"
print("The input string is:",myStr)
strLen=len(myStr)
range_obj=range(strLen)
character="F"
for index in range_obj:
if myStr[index]==character:
print("The character {} is at index {}".format(character,index))
break
Output:
The input string is: Python For Beginners
The character F is at index 7
Find All the Occurrences of a Character in a String Using for Loop
To find all the occurrences of the character in the string, we will remove the break statement from the for loop. Due to this, the for loop will check all the characters and print their positions if the character is the one we are looking for. You can observe this in the following example.
myStr="Python For Beginners"
print("The input string is:",myStr)
strLen=len(myStr)
range_obj=range(strLen)
character="n"
for index in range_obj:
if myStr[index]==character:
print("The character {} is at index {}".format(character,index))
Output:
The input string is: Python For Beginners
The character n is at index 5
The character n is at index 15
The character n is at index 16
Find the Rightmost Index of the Character in a String Using for Loop
To find the rightmost position of the character, we need to modify our approach. For this, we will create a variable named position
to store the position of the character in the string instead of printing the position. While iterating through the characters of the string, we will keep updating the position
variable whenever we find the desired character.
After execution of the for loop, we will get the index of the rightmost occurrence of the character in the string. You can observe this in the following example.
myStr="Python For Beginners"
print("The input string is:",myStr)
strLen=len(myStr)
range_obj=range(strLen)
character="n"
position=-1
for index in range_obj:
if myStr[index]==character:
position=index
print("The character {} is at rightmost index {}".format(character,position))
Output:
The input string is: Python For Beginners
The character n is at rightmost index 16
The above approach gives the position of the character starting from the left side. If you want to get the position of the rightmost occurrence of the character in the string, you can use the following approach.
myStr="Python For Beginners"
print("The input string is:",myStr)
strLen=len(myStr)
range_obj=range(strLen)
character="n"
for index in range_obj:
if myStr[strLen-index-1]==character:
print("The character {} is at position {} from right.".format(character,index+1))
break
Output:
The input string is: Python For Beginners
The character n is at position 4 from right.
In this example, we have used the string indexing operator to access elements from the right side of the string. Hence, we can get the rightmost position of any given character with minimum executions of the for loop.
Find the Position of a Character in a String Using While Loop
Instead of the for loop, you can use a while loop to find the position of a character in the string. For this, we will use the following steps.
- First, we will define a variable named
position
and initialize it to -1. - Then, we will find the length of the string using the
len()
function. - Now, we will use a while to iterate through the characters of the string. We will define the exit condition if the position variable becomes greater than or equal to the length of the string.
- Inside the while loop, we will first increment the
position
variable. Next, we will check if the character in the current position is the character we are looking for or not. If yes, we will print the position of the character and exit the while loop using the break statement. Otherwise, we will move to next character.
After execution of the while loop, we will get the index of the first occurrence of the character in the string. You can observe this in the following code.
myStr="Python For Beginners"
print("The input string is:",myStr)
strLen=len(myStr)
character="n"
position=-1
while position<strLen-1:
if myStr[position+1]==character:
print("The character {} is at position {}.".format(character,position+1))
break
position+=1
Output:
The input string is: Python For Beginners
The character n is at position 5.
Find All the Occurrences of a Character in a String Using While Loop
If you want to find all the occurrences of the character in the string, you can remove the break statement from the while loop. After this, the program will print all the positions of the character in the given string as shown below.
myStr="Python For Beginners"
print("The input string is:",myStr)
strLen=len(myStr)
character="n"
position=-1
while position<strLen-1:
if myStr[position+1]==character:
print("The character {} is at position {}.".format(character,position+1))
position+=1
Output:
The input string is: Python For Beginners
The character n is at position 5.
The character n is at position 15.
The character n is at position 16.
Find the Rightmost Index of the Character in a String Using While Loop
To find the rightmost position of the character using the while loop in Python, we will create a variable named rposition
to store the position of the character in the string instead of printing the position. While iterating through the characters of the string using the while loop, we will keep updating the rposition
variable whenever we find the desired character.
After execution of the while loop, we will get the position of the rightmost occurrence of the character in the string. You can observe this in the following example.
myStr="Python For Beginners"
print("The input string is:",myStr)
strLen=len(myStr)
character="n"
position=-1
rposition=0
while position<strLen-1:
if myStr[position+1]==character:
rposition=position+1
position+=1
print("The character {} is at rightmost position {}.".format(character,rposition))
Output:
The input string is: Python For Beginners
The character n is at rightmost position 16.
The above approach gives the position of the character starting from the left side. If you want to get the position of the rightmost occurrence of the character in the string, you can use the following approach.
myStr="Python For Beginners"
print("The input string is:",myStr)
strLen=len(myStr)
character="n"
position=0
while position<strLen-1:
if myStr[strLen-position-1]==character:
print("The character {} is at position {} from right.".format(character,position+1))
break
position+=1
Output:
The input string is: Python For Beginners
The character n is at position 4 from right.
Find the Position of a Character in a String Using the find() Method
To find the position of a character in a string, you can also use the find()
method. The find()
method, when invoked on a string, takes a character as its input argument. After execution, it returns the position of the first occurrence of the character in the string. You can observe this in the following example.
myStr="Python For Beginners"
print("The input string is:",myStr)
strLen=len(myStr)
character="n"
position=myStr.find(character)
print("The character {} is at position {}.".format(character,position))
Output:
The input string is: Python For Beginners
The character n is at position 5.
Suggested Reading: Create a chat application in Python
Find the Index of a Character in a String Using the index() Method
The index()
method is used to find the index of a character in a string. The index()
method, when invoked on a string, takes a character as its input argument. After execution, it returns the position of the first occurrence of the character in the string. You can observe this in the following example.
myStr="Python For Beginners"
print("The input string is:",myStr)
strLen=len(myStr)
character="n"
position=myStr.index(character)
print("The character {} is at index {}.".format(character,position))
Output:
The input string is: Python For Beginners
The character n is at index 5.
Find the Rightmost Index of a String in Python Using the rfind() Method
To find the rightmost position of a character in a python string, you can also use the rfind()
method. The rfind()
method works in a similar manner to the find()
method except that it returns the rightmost position of the input character in the string. You can observe this in the following example.
myStr="Python For Beginners"
print("The input string is:",myStr)
strLen=len(myStr)
character="n"
position=myStr.rfind(character)
print("The character {} is at rightmost position {} .".format(character,position))
Output:
The input string is: Python For Beginners
The character n is at rightmost position 16 .
Conclusion
In this article, we have discussed different ways to find the position of a character in a string in Python. To read more about this topic, you can read this article on how to find all the occurrences of a substring in a string.
You might also like this article on python simplehttpserver.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.