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

Write a Python program to find the Last Occurrence of a Character in a String with a practical example. This python program allows the user to enter a string and a character.

# Python Program to find Last Occurrence of a Character in a String

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

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

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

First, we used For Loop to iterate each character in a String. Inside that, we used the If statement to check whether any character in str1 string is equal to the given character or not. If true, then flag = i.

Next, we used the If Else Statement to check whether the flag value is equal to -1 or not 0.

string = hello world
ch = l
flag = -1

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 = 2

Do the same for the remaining iterations. Here, the condition (flag == -1) is False. So, print inside the else blocks executed.

Python Program to find Last Occurrence of a Character in a String Example 2

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

# Python Program to find Last Occurrence of a Character in a String

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

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

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

Python last character occurrence in a string output

Please enter your own String : tutorialgateway
Please enter your own Character : t
The Last Occurrence of  t  is Found at Position  11

Python Program to find Last Occurrence in a String Example 3

This Python last occurrence of a character in a string is the same as the first example. But, this time, we used the Functions concept to separate the python program logic.

# Python Program to find Last Occurrence of a Character in a String

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

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

flag = last_Occurrence(ch, str1)

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

Python last character occurrence in a string output

Please enter your own String : hello
Please enter your own Character : m
Sorry! We haven't found the Search Character in this string 
>>> 
Please enter your own String : python programs
Please enter your own Character : p
The Last Occurrence of  p  is Found at Position  8
>>>