Python program to remove first occurrence of a character in a string; Through this tutorial, i am going to show you how to remove first occurrence of a character in a string.
In this tutorial, i will write a python programs to remove first occurrence of a character in a string.
How to remove first occurrence of character from string in python
- To Remove the First Occurrence of a Character in a String using For Loop in Python
- To Delete the First Occurrence of a Character in a String using For While Loop in Python
- To Delete First Occurrence of a String Character using function
To Remove the First Occurrence of a Character in a String using For Loop in Python
- Get input string from user in program
- Next, it finds and removes the first occurrence of that character inside a given string using For Loop.
- Use For Loop to iterate each character in a String.
- Inside the For Loop, use If Statement to check the character is equal to ch or not. If true, it uses the string slice index to remove that character and Break statement to exit the loop.
# Python Program to Remove the First Occurrence of a Character in a String string = input("Please enter your own String : ") char = input("Please enter your own Character : ") string2 = '' length = len(string) for i in range(length): if(string[i] == char): string2 = string[0:i] + string[i + 1:length] break print("Original String : ", string) print("Final String : ", string2)
After executing the program, the output will be:
Please enter your own String : hello Please enter your own Character : o Original String : hello Final String : hell
To Delete the First Occurrence of a Character in a String Using While loop in Python
- Get input string from user in prgraom
- Next, it finds and removes the first occurrence of that character inside a given string using while Loop.
- Use while Loop to iterate each character in a String.
- Inside the while Loop, use If Statement to check the character is equal to ch or not. If true, it uses the string slice index to remove that character and Break statement to exit the loop.
# Python Program to Remove First Occurrence of a Character in a String string = input("Please enter your own String : ") char = input("Please enter your own 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("Original String : ", string) print("Final String : ", string2)
After executing the program, the output will be:
Please enter your own String : string Please enter your own Character : g Original String : string Final String : strin
To Delete First Occurrence of a String Character using Function
- Define function in program to remove first occurrence of string character.
- Get input string from user in program
- Call function and remove first occurrence of string character.
- Print result.
# Python Program to Remove First Occurrence of a Character in a String def removeFirstOccur(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 str1 = input("Please enter your own String : ") char = input("Please enter your own Character : ") print("Original String : ", str1) print("Final String : ", removeFirstOccur(str1, char))
After executing the program, the output will be:
Please enter your own String : world Please enter your own Character : d Original String : world Final String : worl
Be First to Comment