Python Program to find First Occurrence of a Character in a String

Write a Python program to find the first occurrence of a character in a string with a practical example.

python program to find first occurrence of a character in a string Example 1

This python program allows the user to enter a string and a character.

Please refer String article to understand everything about Python Strings.

# Python Program to check First Occurrence of a Character in a String

string = input("Please enter your own String : ")
char = input("Please enter your own Character : ")

flag = 0
for i in range(len(string)):
    if(string[i] == char):
        flag = 1
        break

if(flag == 0):
    print("Sorry! We haven't found the Search Character in this string ")
else:
    print("The first Occurrence of ", char, " is Found at Position " , i + 1)

Python first character occurrence in a string output

Please enter your own String : hello world
Please enter your own Character : l
The first Occurrence of  l  is Found at Position  3

Here, we used For Loop to iterate every character in a String. Inside the For Loop, we used the If statement to check whether any character in str1 string is equal to character ch or not. If true, then flag = 1, and Break statement executed.

string = hello world
ch = l
flag = 0

For Loop First Iteration: for i in range(11)
if(string[i] == char)
if(h == l) – Condition is false.

Second Iteration: for 1 in range(11)
if(e == l) – Condition is false.

Third Iteration: for 2 in range(11)
if(str[2] == ch) => if(l == l) – Condition is True.

Flag  = 1 and break statement exit from the loop. Next, we used the If Else Statement to check whether the flag value is equal to 0. Here, the condition is False. So, print inside the else blocks executed.

Python Program to find first occurrence of a character in a string Example 2

This Python first occurrence of a character program is the same as the above. However, we just replaced the For Loop with While Loop.

# Python Program to check First Occurrence of a Character in a String

string = input("Please enter your own String : ")
char = input("Please enter your own Character : ")
i = 0
flag = 0

while(i < len(string)):
    if(string[i] == char):
        flag = 1
        break
    i = i + 1

if(flag == 0):
    print("Sorry! We haven't found the Search Character in this string ")
else:
    print("The first Occurrence of ", char, " is Found at Position " , i + 1)

Python first character occurrence in a string output

Please enter your own String : python programming
Please enter your own Character : o
The first Occurrence of  o  is Found at Position  5

Python program to get first occurrence of a character in a string Example 3

This python program to find the First Occurrence of a Character in a String is the same as the first example. However, this time, we used the Functions concept to separate the logic.

# Python Program to check First Occurrence of a Character in a String

def first_Occurrence(char, string):
    for i in range(len(string)):
        if(string[i] == char):
            return i
    return -1

str1 = input("Please enter your own String : ")
ch = input("Please enter your own Character : ")

flag =  first_Occurrence(ch, str1)
if(flag == -1):
    print("Sorry! We haven't found the Search Character in this string ")
else:
    print("The first Occurrence of ", ch, " is Found at Position " , flag + 1)
Python Program to find First Occurrence of a Character in a String 3