;

Python Program to Find the Last Occurrence of a Character in a Given String


Tutorialsrack 27/04/2020 Python

In this Python program, we will learn how to find the last occurrence of a character in a given string.

Here is the source code of the program to find the last occurrence of a character in a given string.

Program 1: Python Program to Find the Last Occurrence of a Character in a Given String Using For Loop with Range

Python Program to Find the Last Occurrence of a Character in a Given String Using For Loop with Range
# Python Program to Find the Last Occurrence of a Character in a Given String Using For Loop with Range

# Take the Input From the User
string = input("Enter The String: ")
char = input("Enter a Character to Check its Last Occurrence: ")

# make string to lower to avoid case-sensitiveness of a character
string=string.lower()

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)
Output

Enter The String: Tutorials Rack is an Online Learning Website

Enter a Character to Check its Last Occurrence: t

The Last Occurrence of  t  is Found at Position  43

Program 2: Python Program to Find the Last Occurrence of a Character in a Given String Using While Loop

Python Program to Find the Last Occurrence of a Character in a Given String Using While Loop

# Python Program to Find the Last Occurrence of a Character in a Given String Using While Loop

# Take the Input From the User
string = input("Enter The String: ")
char = input("Enter a Character to Check its Last Occurrence: ")

# make string to lower to avoid case-sensitiveness of a character
string=string.lower()

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)
Output

Enter The String: Tutorials Rack is an Online Learning Website

Enter a Character to Check its Last Occurrence: r

The Last Occurrence of  r  is Found at Position  32

Program 3: Python Program to Find the Last Occurrence of a Character in a Given String By defining a function

Python Program to Find the Last Occurrence of a Character in a Given String By defining a function

# Python Program to Find the Last Occurrence of a Character in a Given String By defining a function

# Defining a Function
def last_Occurrence_char(char, string):
    # make string to lower to avoid case-sensitiveness of a character
    string = string.lower()
    index = -1
    for i in range(len(string)):
        if(string[i] == char):
            index = i
    return index

# Take the Input From the User
string = input("Enter The String: ")
char = input("Enter a Character to Check its Last Occurrence: ")

flag = last_Occurrence_char(char, string)

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)
Output

Enter The String: Tutorials Rack is an Online Learning Website

Enter a Character to Check its Last Occurrence: n

The Last Occurrence of  n  is Found at Position  35


Related Posts



Comments

Recent Posts
Tags