Python Program to Remove First Occurrence of Character in String

Python Program to Remove First Occurrence of Character in String

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

  1. Python Program to Convert Uppercase to Lowercase
  2. Convert String Lowercase to Uppercase in Python
  3. Python First Character of String Uppercase
  4. Python Concatenate String and Variable (int, float, etc)
  5. How to Find Length of String in Python
  6. Python: String Join Function
  7. Python String Append Examples
  8. How to replace a character in a string in python
  9. Python – Count Number of Occurrences in String
  10. Python Remove Last Character from String
  11. Python Remove Character From String
  12. Python Program to Reverse String
  13. Python: Remove Special Characters from String
  14. Python Program Count vowels in String
  15. Python Split a String into Array of Characters, Space
  16. Python Program to Swap Two Character of Given String
  17. Python Program String Find
  18. Python: Two Given Strings and Swap First Two Characters of Each String
  19. Print All Permutations of Given String in Python
  20. Python | Check Whether a String Contains Number or Letter
  21. Python: Convert String to Float & Float to String
  22. Python Program to Reverse String Using Stack
  23. How To Convert Python Int to String and String to Int
  24. Python Convert String to List and List to String

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *