In this tutorial we will look at how to remove a character from a string in Python. But before we proceed, a quick question –
Can you remove characters from strings in Python?
Not exactly. Strings are immutable in python and hence cannot be changed. You can, however, create a new string with the required character removed and this is exactly what we will cover in this tutorial. Let’s look at the different use-cases of removing characters from strings with the help of examples.
Remove all occurrences of a character in string
If you want to remove all occurrences of a character in a string in Python, you can use the string replace() function or string translate() function with a translation table. Both these methods can return a new string with all the occurrences of the character removed. Let’s look at them through examples –
1. Using string replace()
The string replace()
function is used to replace the occurrences of a substring by a different substring. To remove the occurrence of a character, you can replace it with an empty string.
s = "abbcccdddd" # reomve 'c' from s print(s.replace("c", ""))
Output:
abbdddd
You can see that the returned string has all the occurrences of “c” removed.
2. Using string translate()
You can also create a translation table and use str.translate()
to remove occurrences of a character in a string. A translation table is basically a mapping used to replace strings with appropriate values.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
s = "abbcccdddd" # reomve 'c' from s print(s.translate({ord("c"): None}))
Output:
abbdddd
You can see that all the occurrences of “c” have been removed in the returned string. Note that we passed a translation table as a dictionary mapping the Unicode for the character “c” to None
to the translate() function. The ord()
function returns an integer representing the Unicode code of the given character.
For more on the translate() function, refer to its documentation.
Remove character from string based on index
If you want to remove a character based on its index in a string, you can use a combination of slicing and string concatenation to remove that character in a new string. Here, we create the substrings to the left and right of the character by slicing the original string and then concatenate the two to create a new string with the character removed.
s = "abcdef" # remove charater at the 2nd index i = 2 new_s = s[:i] + s[i+1:] # print the string print(new_s)
Output:
abdef
You can see that the resulting string has the character “c” at index 2 from the original string removed. Note that this method only removes the character at a specific index and does not remove all the occurrences of the particular character. For example –
s = "abcdefc" # remove charater at the 2nd index i = 2 new_s = s[:i] + s[i+1:] # print the string print(new_s)
Output:
abdefc
You can see that only the character at index 2, which happened to be “c” was removed. Other characters including other occurrences of “c”, were not removed.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Tutorials on python strings:
- Python – Trim Whitespace Characters from Strings
- Python – Reverse a String with Examples
- Python – Remove Character From String
- Python – Frequency of each word in String
- Python – Convert List to a String
- Python String Count – With Examples
- New String Functions in Python 3.9
- Python String Find – With Examples
- Python String Startswith – With Examples
- Python String Endswith – With Examples
- Python String Split – With Examples
- Python String Join – With Examples
- Python String Concatenation – With Examples
- Python String Replace – With Examples
- Python String Uppercase – With Examples
- Python String Lowercase – With Examples
- Python String Strip – With Examples