Given a string and a character, your task is to find the first position of the character in the string using Python. These types of problems 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: Get the position of a character in Python using rfind()
Python String rfind() method returns the highest index of the substring if found in the given string. If not found then it returns -1.
Python3
string = 'Geeks'
letter = 'k'
print (string.rfind(letter))
|
Method 2: Get the position of a character in Python using regex
re.search() method either returns None (if the pattern doesn’t match) or re.MatchObject contains information about the matching part of the string. This method stops after the first match,
Python3
import re
string = 'Geeksforgeeks'
pattern = 'for'
match = (re.search(pattern, string))
print ( "starting index" , match.start())
print ( "start and end index" , match.span())
|
Outputstarting index 5
start and end index (5, 8)
Method 3: Get the position of a character in Python using index()
This Method raises Value Error in case the character is not present
Python3
ini_string1 = 'xyze'
c = "b"
print ( "initial_strings : " , ini_string1,
"\ncharacter_to_find : " , c)
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 character available in string {}" . format (ini_string1))
|
Output:
initial_strings : xyze
character_to_find : b
No such character available in string xyze
Method 4: Get the position of a character in Python using the loop
In this example, we will use the Python loop to find the position of a character in a given string.
Python3
ini_string = 'abcdef'
c = "b"
print ( "initial_string : " , ini_string, "\ncharacter_to_find : " , c)
res = None
for 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)))
|
Output:
initial_string : abcdef
character_to_find : b
Character b is present at 2
Time Complexity: O(n), where n is length of string.
Auxiliary Space: O(1)
Method 5: Get the position of a character in Python using str.find
This method returns -1 in case the character is not present.
Python3
ini_string = 'abcdef'
ini_string2 = 'xyze'
c = "b"
print ( "initial_strings : " , ini_string, " " ,
ini_string2, "\ncharacter_to_find : " , c)
res1 = 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 6: Get the position of a character in Python using a list comprehension and next function
This method involves using a list comprehension to create a list of tuples containing the index and character for each character in the string. We can then use the next function to return the first tuple whose character matches the one we are searching for. If the character is not found, we raise a ValueError.
Python3
def find_position(string, char):
try :
return 1 + next (i for i, c in enumerate (string) if c = = char)
except :
return - 1
string = 'xybze'
char = 'b'
print ( "initial_strings : " , string)
print ( "character_to_find : " , char)
print (find_position(string, char))
|
Outputinitial_strings : xybze
character_to_find : b
3
Time complexity: O(n)
Auxiliary Space: O(n)