Find Character Position in String : Best 3 Methods

Find Character Position in String

Most of the coders or developers use string in their code. The mostly purpose of it is to document the function and the coding blocks. You also use string variables in the code. Let’s say you want to find the character position in the string, then how you will do that? In this tutorial you will know the various methods to find the index of a character in a string.

Methods to Find Character Position in String

Let’s learn how to find a character position in a string using the different methods.

Method 1: Using the index() function

The first method to find the position of the character in string is the use of str.find() function. Here you will just pass the character as an argument to find its position. If the character is not found then it will return the ValueError: substring not found.

Run the below lines of code to find the index of the character.

string = "Data Science Learner"
character = "L"
position = string.index(character)
print(position) 

Output

position of the character using the index function
position of the character using the index function

Method 2: Using the find() method

You can also use the find() function to find the position of the character. Here you will also pass the character as an argument to the str.find() function. Execute the below lines of code to find the index of the character.

string = "Data Science Learner"
character = "L"
position = string.find(character)
print(position) 

Output

position of the character using the find function
position of the character using the find function

Method 3: Find the Character Position in the String using the loop

In this method, I will use a loop on the entire string to find the character position. You will use the loop to compare each character with the character you want to find the position. If it is found then it breaks the loop and returns the index of the character.

Run the below lines of code and see the output.

string = "Data Science Learner"
character = "L"
position = None
for i in range(len(string)):
    if string[i] == character:
        position = i
        break
print(position)

Output

position of the character using loop
position of the character using loop

Conclusion

Sometimes you have to find the pattern in the string or character in the string for text extraction, text analysis, etc. The above methods will allow you to find the index of the character in the string.  You can use any one of them at per convenience.

I hope you have liked this tutorial. If you have any queries about this content then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
Share via
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner