Removing first occurrence of character in a string using Python

In this tutorial, we will learn to remove the first occurrence of a character in a string using Python. So, the task is to delete the character in a string at its first occurrence. Firstly, we have to find the character in the string, verify that it is the first occurrence. And finally, remove the character and display the resultant string. If you are looking for the Python program to delete the first occurrence of a character in a string, you are at the right place.

Method to remove the first occurrence of a character

For a better understanding of the method, let us take a string – “A small string for better understanding”. Firstly, let us assume the character to be removed is ‘s’. So, to remove the first occurrence of this character follow the following steps-

  • Traverse the string from starting character by character.
  • Compare each character of the string with the character to be removed.
  • If the compare returns success, store the substrings on the left and right of the character after combining in another variable.
  • Otherwise, continue the above steps again until the compare returns success.
  • Finally, display the resulting string as an output.

Example –

  • String – “A small string for better understanding”
  • Character – ‘s’
  • Here we find the first occurrence of the character ‘s’ at 3rd position in the string.
  • The substrings on the left and right are – “A “ and “mall string for better understanding”.
  • After combining these two substrings, the resulting string is – “A mall string for better understanding”.

Removing the first occurrence of a character in a string using Python

Now, we will see a Python program that removes the first occurrence of a character in a string. We take the string and the character from the user as an input. Then we traverse and search the character in the string using a ‘for’ loop. If we find the character, then we store the substrings on the left and right of that character. Finally, we display the resultant string as an output. The Python program is given below-

text = input("ENTER TEXT : ")
char = input("ENTER CHARACTER FOR DELETION : ")
result_text = ''
for i in range(len(text)):
    if(text[i] == char):
        result_text = text[0:i] + text[i + 1:len(text)]
        break
print("ENTERED TEXT        : ",text)
print("TEXT AFTER DELETION : ",result_text)

Here, we store the string and character in variable ‘text’ and ‘char’ respectively. During traversal of the string, if we find the character we store the string in ‘result_text’ after removal of that character and terminate the loop. finally, we display the desired string as an output to the user.

Python program output

The program removes the first occurrence of a character in a string as given below-

[email protected]:~/python$ python3 occurence.py
ENTER TEXT : Python is advanced language
ENTER CHARACTER FOR DELETION : a
ENTERED TEXT        :  Python is advanced language
TEXT AFTER DELETION :  Python is dvanced language
[email protected]:~/python$

Here, the string is “Python is advanced language”. And the character to be removed is ‘a’. So, the first occurrence of ‘a’ is in the word “advanced”. Hence, the first occurrence of character ‘a’ is at position 11 in the string. Finally, the resultant string is displayed.

Thank you for reading this tutorial. I hope it helps you.

Also read: Python string.punctuation | Get all sets of punctuation

Leave a Reply

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