Get position of a character in a string in Python

In this article, we will learn to find the position of a character in a string in python programming language.

Table Of Contents

Get position of a character in String using For Loop

We can find the postiion of a given char from in a given string using for loop. In this method we will iterate through the entire string and postion at which character matches will be stored in a var. See the example below :

EXAMPLE :

alphabets_var = 'abcdefghijklmnopqrstuvwxyz'
pos_var = None

# len(alphabets_var) is the length of alphabets_var or
# it will give till which point the loop will iterate
for i in range(0,len(alphabets_var)):  
    if alphabets_var[i] == 'j':
        pos_var = i + 1
        break

print(pos_var)

OUTPUT :

10

In example above, you can see that in alphabets_var we have alphabets from a to z. Loop starts from index 0 and iterates till lenght of the variable alphabets_var and checks every single char that if mathces with the given char or not. If found it will store the postion of the character matched.

Get position of a character in String using find() method

In this technique, to get the position of a character in a string in Python we will use the find() method. It is a built in method of string class, which takes a character whose position needs to be found out as a parameter. It returns -1 if the character is not found.
It gives the index position of first occurrence of character in the string. See the example below :

EXAMPLE :

alphabets_var = 'abcdefghijklmnopqrstuvwxyz'

# Find the position of character in string 
pos_var = alphabets_var.find('f')

print(pos_var + 1)

OUTPUT :

6

In code above you can see we have successfully found the position of alphabet f which is 6. You may notice, pos_var+1 has been used. Because index of strings starts with 0 so position should be +1.

Get position of a character in String using index() method

In this technique, we will be using the index() functon to get the position of a character in a string. It is a string class method and accepts a character whose position needs to be found. In this method we need to do error handling because it can raise a ValueError if the given character does not exist in the string. Lets see an Example of this method :

EXAMPLE :

alphabets_var = 'abcdefghijklmnopqrstuvwxyz'

try :
    # Find the position of character in string 
    pos_var = alphabets_var.index('z')
    print('Character present at position: ', pos_var+1)
except ValueError :
    print('Character not found')

OUTPUT :

Character present at position:  26

In code above you can see we have used index() method to find out the position of alphabet z which is 26. Also we except a ValueError if the character is not found in the string.

Get position of a character in String using rfind() method

Next method that we will be using to get the position of a character in a string is rfind() method. It is also a built in Python method of string class, similar to the find() method. It takes a character as an argument and returns 0 if character is not found in the string. Otherwise returns the index poistion of the character in string. But it looks from right to left in the string, therefore gives the index position of last occurrence of character in string. Lets see an example

EXAMPLE :

alphabets_var = 'abcdefghijklmnopqrstuvwxyz'

# Find the position of last occurrence of character in string 
pos_var = alphabets_var.rfind('g') + 1

print("Character found at position: ", pos_var)

OUTPUT :

Character found at position:  7

In code above you can see if have successfully found the position of char g which is 7 and position of g has been stored in variable pos_var.

Summary

So we have learned about four different methods through which we can get the position of a character in a string using the Python Programming language. Most practical and useful method is using the method 3 which is index() method because it also gives the option to handle errors. If any char is not found so instead of any error in terminal(ValueError) or any integer (0 in case of rfind() method and -1 in case of find() method) we can get a message.
We can also use other methods depending upon our needs. Most easiest way to get the position of a character in a string is find() method which simply returns position-1 (because index starts from zero) and returns -1 if not found.

Make sure to run these codes on your machine and play around with code. Also I have used Python 3.10.1 for writing example codes. To check your version write python –version in your terminal.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top