remove first occurrence of a character from string in python

Python – Remove First Occurrence of Character in String

In this tutorial, we will look at how to remove the first occurrence of a character from a string in Python with the help of some examples.

remove first occurrence of a character from string in python

Strings are immutable in Python. That is, they cannot be modified after they are created. You can, however, create a copy of the original string with the first occurrence of the character removed.

To remove the first occurrence of a character from a string –

  1. Use the string index() function to get the index of the first occurrence. Note that if the character is not present in the string, it will raise a ValueError.
  2. Then, use the above index to slice the original string such that the character at that index is skipped.

Let’s look at an example –

# create a string
s = "Two cryons, a pen, a pencil, and a sharpner."
# get index of first occurrence of 'a'
i = s.index('a')
# remove the first occurrence of 'a'
print(s[:i]+s[i+1:])

Output:

Two cryons,  pen, a pencil, and a sharpner.

You can see that the output string does not contain the first occurrence of the character ‘a’ from the original string. Also, notice that the other occurrences of the character ‘a’ in the string are unaffected.

For more on the Python string index() function, refer to its documentation.

Alternatively, you can also use a loop to iterate through the characters of the original string and remove the first occurrence of a character from a string in Python. Use the following steps –

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

  1. Create an empty string to store our result and a flag set to False to determine whether we have encountered the character that we want to remove.
  2. Iterate through each character in the original string.
  3. For each character, check if it’s equal to the character we want to remove and whether the flag is False. If both the conditions are true, set the flag to True and skip over to the next iteration. Else, add the character to our result string.

Let’s look at an example –

# create a string
s = "Two cryons, a pen, a pencil, and a sharpner."

result = ""
ch_to_remove = 'a'
occurred_flag = False

# iterate over each character in s
for ch in s:
    if ch == ch_to_remove and occurred_flag == False:
        occurred_flag = True
        continue
    else:
        result += ch

# display the resulting string
print(result)

Output:

Two cryons,  pen, a pencil, and a sharpner.

We get the same result as above. The resulting string has the first instance of the character removed.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top