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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Python3
# Python3 code to demonstrate# to find the first position of the character# in a given string# Initializing stringini_string = 'abcdef'# Character to findc = "b"# printing initial string and characterprint ("initial_string : ", ini_string, "\ncharacter_to_find : ", c)# Using Naive Methodres = Nonefor i in range(0, len(ini_string)): if ini_string[i] == c: res = i + 1 break if res == None: print ("No such character 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
# Python3 code to demonstrate# to find first position of character# in a given string# Initializing stringini_string = 'abcdef'ini_string2 = 'xyze'# Character to findc = "b"# printing initial string and characterprint ("initial_strings : ", ini_string, " ", ini_string2, "\ncharacter_to_find : ", c)# Using find Methodres1 = ini_string.find(c)res2 = ini_string2.find(c) if res1 == -1: print ("No such character 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 character available in string {}".format( ini_string2))else: print ("Character {} in string {} is present at {}".format( c, ini_string2, str(res2 + 1))) |
Output:
initial_strings : abcdef xyze character_to_find : b Character b in string abcdef is present at 2 No such character available in string xyze
Method #3: Using index()
This Method raises Value Error in case if character not present
Python3
# Python3 code to demonstrate# to find first position of character# in a given string# Initializing stringini_string1 = 'xyze'# Character to findc = "b"# printing initial string and characterprint ("initial_strings : ", ini_string1, "\ncharacter_to_find : ", c)# Using index Methodtry: 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 character available in string {}".format(ini_string1)) |
Output:
initial_strings : xyze character_to_find : b No such character available in string xyze

