;

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


Tutorialsrack 24/04/2020 Python

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

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

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

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

# Take the Input from the User
string = input("Enter the String: ")
char = input("Enter the Character: ")

string2 = ''
length = len(string)

for i in range(length):
    if(string[i] == char):
        string2 = string[0:i] + string[i + 1:length]
 
print("\nOriginal String Before Removing Last Occurrence of Character:  ", string)
print("Final String After Removing Last Occurrence of Character:      ", string2)
Output

Enter the String: Tutorialsrack is an online learning website

Enter the Character: r

 

Original String Before Removing Last Occurrence of Character:   Tutorialsrack is an online learning website

Final String After Removing Last Occurrence of Character:       Tutorialsrack is an online learning website

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

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

# Python Program to Remove 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 the Character: ")

string2 = ''
length = len(string)
i = 0

while(i < length):
    if(string[i] == char):
        string2 = string[0 : i] + string[i + 1 : length]
    i = i + 1
 
print("\nOriginal String Before Removing Last Occurrence of Character:  ", string)
print("Final String After Removing Last Occurrence of Character:      ", string2)
Output

Enter the String: Tutorialsrack is an online learning website

Enter the Character: o

 

Original String Before Removing Last Occurrence of Character:   Tutorialsrack is an online learning website

Final String After Removing Last Occurrence of Character:       Tutorialsrack is an nline learning website

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

Python Program to Remove the Last Occurrence of a Character in a Given String By defining Function

# Python Program to Remove the Last Occurrence of a Character in a Given String By defining Function

# Definig a Function
def removeLastOccur(string, char):
    string2 = ''
    length = len(string)
    i = 0

    while(i < length):
        if(string[i] == char):
            string2 = string[0 : i] + string[i + 1 : length]
        i = i + 1
    return string2
 
# Take the Input from the User
string = input("Enter the String: ")
char = input("Enter the Character: ")

print("\nOriginal String Before Removing Last Occurrence of Character:  ", string)
print("Final String After Removing Last Occurrence of Character:      ", removeLastOccur(string, char))
Output

Enter the String: Tutorialsrack is an online learning website

Enter the Character: i

 

Original String Before Removing Last Occurrence of Character:   Tutorialsrack is an online learning website

Final String After Removing Last Occurrence of Character:       Tutorialsrack is an online learning webste


Related Posts



Comments

Recent Posts
Tags