Given a string and a character, your task is to find the first position of the character in the string. These types of problem are very competitive programming where you need to locate the position of the character in a string.
Let’s discuss a few methods to solve the problem.
Method #1: Using Naive Method
initial_string : abcdef character_to_find : b Character b is present at 2
Method #2: Using find
This method returns -1 in case character not present.
initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such charater available in string xyze
Method #3: Using index()
This Method raises ValueError in case if character not present
initial_strings : xyze character_to_find : b No such charater available in string xyze
- Python | Find frequency of given character at every position in list of lists
- Python program to find occurrence to each character in given string
- Python | Lowercase first character of String
- How to capitalize first character of string in Python
- Python | Frequency of each character in String
- Python | K Character Split String
- Python | Split string on Kth Occurrence of Character
- Python | Count occurrences of a character in string
- Python program for removing i-th character from a string
- Python - Group list by first character of string
- Python | Removing newline character from string
- Ways to remove i'th character from string in Python
- Python - Convert Alternate String Character to Integer
- Python | Check order of character in string using OrderedDict( )
- Python | Construct string from character frequency tuple
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.