How to Delete a Character from a String in Python?

In this python tutorial, you will learn how to delete a character from a string.

Table Of Contents

A string is a set of characters. Let’s discuss some approaches to delete a character from a string in Python.

Delete a character from a string using replace()

The replace() method of string class is used to replace a character from a string with other characters. So it is possible to delete a character from string by replacing the character with an empty string i.e. “”. In this way, we can delete a particular character.

Syntax:

Advertisements
input_str.replace(character, "")

Where, input_str is the input string.

Parameters:

  1. A character that needs to be deleted from the string.
  2. The replacement string.

As our replacement string is an empty string, so it deletes the character from the string.

Example:

In this example, we will remove

  1. character – c
  2. character W.
# Consider the below string
input_str="Welcome to thispointer"

# Display the actual string
print(input_str)

# Delete character 'c' from input_str
input_str = input_str.replace("c", "")

print("After deleting character-c: ", input_str)

# Delete character 'W' from input_str
input_str = input_str.replace("W", "")

print("After deleting character-W: ", input_str)

Output:

Welcome to thispointer
After deleting character-c:  Welome to thispointer
After deleting character-W:  elome to thispointer

We can see that charActer – c and W are deleted from the string.

Delete a character from a string using translate()

We can delete a particular character from a string using the translate() method. It takes a character to be deleted as a parameter through ord() and set that character to None.

Syntax:

input_str.translate({ord(character): None})

Where input_str is the input string. The ord() function returns the ASCII value of given character and it gets mapped to None. So, translate() function will remove all occurrences of this character from the string.

Example:

In this example, we will delete following characters from the string,

  1. charcater-e
  2. charcater-r
  3. charcater-i
  4. charcater-t
  5. charcater-p

For example,

# Consider the below string
input_str="Welcome to thispointer"

# Delete  character-e  from input_str
print("After deleting character-e: ",input_str.translate({ord('e'): None}))

# Delete  character-r from input_str
print("After deleting character-r: ",input_str.translate({ord('r'): None}))

# Delete  character-i  from input_str
print("After deleting character-i: ",input_str.translate({ord('i'): None}))

# Delete  character-t from input_str
print("After deleting character-t: ",input_str.translate({ord('t'): None}))

# Delete  character-p  from input_str
print("After deleting character-p: ",input_str.translate({ord('p'): None}))

Output:

After deleting character-e:  Wlcom to thispointr
After deleting character-r:  Welcome to thispointe
After deleting character-i:  Welcome to thsponter
After deleting character-t:  Welcome o hispoiner
After deleting character-p:  Welcome to thisointer

We can see that specified characters are deleted from the string.

Delete a character from a string using the slice operator

We can delete a particular charcater from a string using by selecting indices through the slice operator.

Syntax: to delete the Nth character from the string,

input_str[:n-1] + input_str[n:]

where input_str is the input string and deletes nth character by selecting characters from index position 0 to n-1 and then n+1 till the end of string.

Example:

In this example, we will delete the characters from the string based on index positions i.e.

  1. Delete 4th character from the string.
  2. Delete 7th character from the string.
  3. Delete 14th character from the string.
# Consider the below string
input_str="Welcome to thispointer"

# Delete 4th character
print("After deleting 4th character:",input_str[:3] + input_str[4:] )

# Delete 7th character
print("After deleting 7th character:",input_str[:6] + input_str[7:] )

# Delete 14th character
print("After deleting 14th character:",input_str[:13] + input_str[14:] )

Output:

After deleting 4th character: Welome to thispointer
After deleting 7th character: Welcom to thispointer
After deleting 14th character: Welcome to thspointer

We can see that specified characters were deleted from the string.

Delete the first character from a string using slicing

It is possible to delete the first character by selecting the characters from the second character. So by this, we can exclude the first character.
Syntax:

input_str[1:]

where input_str is the input string.
Example:

In this example, we will delete the first character.

# Consider the below string
input_str="Welcome to thispointer"

# Display the actual string
print(input_str)

# Delete first character from input_str
input_str = input_str[1:]

print("After deleting first character: ", input_str)

Output:

Welcome to thispointer
After deleting first character:  elcome to thispointer

We can see that the first character is removed and the remaining other characters were returned.

Delete the last character from a string using slicing

It is possible to delete the last character by selecting the characters from the first character to one before the last character. So by this way, we can exclude the last character.

Syntax:

input_str[:-1]

Where input_str is the input string.

Example:

In this example, we will delete the last character of a string in Python.

input_str="Welcome to thispointer"

# Display the actual string
print(input_str)

# Delete last character  from input_str
input_str = input_str[:-1]

print("After deleting last character: ", input_str)

Output:

Welcome to thispointer
After deleting  last character:  Welcome to thispointe

We can see that the last character is removed and the remaining other characters were returned.

Summary

In this tutorial, we learnt to delete a character from a string using replace(), and translate() functions. We also learnt to delete the first and last characters from a string by specifying [1:] and [:-1]. Then we looked into a technique to delete a character from a string based on index position.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top