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 | Frequency of each character in String
- Python | Lowercase first character of String
- Python program for removing i-th character from a string
- Python | Count occurrences of a character in string
- Python | Split string on Kth Occurrence of Character
- Ways to remove i'th character from string in Python
- Python | Check order of character in string using OrderedDict( )
- Python Regex | Program to accept string ending with alphanumeric character
- Generate two output strings depending upon occurrence of character in input string in Python
- Python program to find the most occurring character and its count
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Python | Insert character after every character pair
- Python String | find()
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.