To remove the first occurrence of character from string in python; In this python tutorial, we would love to share with you several python programs to find and remove the first occurrence of a character in a string using For Loop, while loop, and functions.
How to remove first occurrence of character from string in python
Follow the below given 3 programs to find and remove first occurrence of a character in a string in python:
- Python Program 1: To Remove the First Occurrence of a Character in a String
- Python Program 2: To Delete the First Occurrence of a Character in a String
- Python Program 3: To Delete First Occurrence of a String Character
Python Program 1: to Remove the First Occurrence of a Character in a String
- Allow the user to enter a string and a character.
- 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
Python Program 2: to Delete the First Occurrence of a Character in a String
- Allow the user to enter a string and a character.
- 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
Python Program 3: to Delete First Occurrence of a String Character
# 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
Recommended Python String Programs
- Python Program to Convert Uppercase to Lowercase
- Convert String Lowercase to Uppercase in Python
- Python First Character of String Uppercase
- Python Concatenate String and Variable (int, float, etc)
- How to Find Length of String in Python
- Python: String Join Function
- Python String Append Examples
- How to replace a character in a string in python
- Python – Count Number of Occurrences in String
- Python Remove Last Character from String
- Python Remove Character From String
- Python Program to Reverse String
- Python: Remove Special Characters from String
- Python Program Count vowels in String
- Python Split a String into Array of Characters, Space
- Python Program to Swap Two Character of Given String
- Python Program String Find
- Python: Two Given Strings and Swap First Two Characters of Each String
- Print All Permutations of Given String in Python
- Python | Check Whether a String Contains Number or Letter
- Python: Convert String to Float & Float to String
- Python Program to Reverse String Using Stack
- How To Convert Python Int to String and String to Int
- Python Convert String to List and List to String