;

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


Tutorialsrack 24/04/2020 Python

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

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

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

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

# Python Program to Remove the First 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]
        break
 
print("\nOriginal String Before Removing First Occurrence of Character:  ", string)
print("Final String After Removing First Occurrence of Character:      ", string2)
Output

Enter the String: Tutorialsrack is an online learning website

Enter the Character: i

 

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

Final String After Removing First Occurrence of Character:       Tutoralsrack is an online learning website

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

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

# Python Program to Remove the First 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]
        break
    i = i + 1
 
print("\nOriginal String Before Removing First Occurrence of Character:  ", string)
print("Final String After Removing First Occurrence of Character:      ", string2)
Output

Enter the String: Tutorialsrack is an online learning website

Enter the Character: a

 

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

Final String After Removing First Occurrence of Character:       Tutorilsrack is an online learning website

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

Python Program to Remove the First Occurrence of a Character in a Given String By Defining Function
# Python Program to Remove First Occurrence of a Character in a Given String By Defining Function

# Define a Function
def removeFirstOccur_char(string, char):
    string2 = ''
    length = len(string)

    for i in range(length):
        if(string[i] == char):
            string2 = string[0:i] + string[i + 1:length]
            break
    return string2

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

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

Enter the String: Tutorialsrack is an online learning website

Enter the Character: a

 

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

Final String After Removing First Occurrence of Character:       Tutorilsrack is an online learning website


Related Posts



Comments

Recent Posts
Tags