Python | Find position of a character in given string
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
# Python3 code to demonstrate # to find the first position of the character # in a given string # Initialising string ini_string = 'abcdef' # Character to find c = "b"# printing initial string and character print ("initial_string : ", ini_string, "\ncharacter_to_find : ", c) # Using Naive Method res = Nonefor i in range(0, len(ini_string)): if ini_string[i] == c: res = i + 1 break if res == None: print ("No such charater available in string") else: print ("Character {} is present at {}".format(c, str(res))) |
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.
# Python3 code to demonstrate # to find first position of character # in a given string # Initialising string ini_string = 'abcdef'ini_string2 = 'xyze' # Character to find c = "b"# printing initial string and character print ("initial_strings : ", ini_string, " ", ini_string2, "\ncharacter_to_find : ", c) # Using find Method res1 = ini_string.find(c) res2 = ini_string2.find(c) if res1 == -1: print ("No such charater available in string {}".format( ini_string)) else: print ("Character {} in string {} is present at {}".format( c, ini_string, str(res1 + 1))) if res2 == -1: print ("No such charater available in string {}".format( ini_string2)) else: print ("Character {} in string {} is present at {}".format( c, ini_string2, str(res2 + 1))) |
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
# Python3 code to demonstrate # to find first position of character # in a given string # Initialising string ini_string1 = 'xyze' # Character to find c = "b"# printing initial string and character print ("initial_strings : ", ini_string1, "\ncharacter_to_find : ", c) # Using index Method try: res = ini_string1.index(c) print ("Character {} in string {} is present at {}".format( c, ini_string1, str(res + 1))) except ValueError as e: print ("No such charater available in string {}".format(ini_string1)) |
initial_strings : xyze character_to_find : b No such charater available in string xyze
Recommended Posts:
- 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
- Python | Frequency of each character in String
- Ways to remove i'th character from string in Python
- Python | Split string on Kth Occurrence of Character
- Python | Count occurrences of a character in string
- Python | Removing newline character from string
- Python program for removing i-th character from a string
- Python | Construct string from character frequency tuple
- 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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

