How to remove a character from a string in Python
Although strings are immutable in Python, you can create new strings on the basis of existing ones. Using different methods, you can remove or replace characters to create the string you want.
What does immutability mean?
Immutability means that once an object has been created, it can no longer be changed. In Python, strings are immutable. When you remove characters from a string using methods in Python, you’re essentially creating a new string with specific elements of the original. The original string remains unchanged though. String immutability aids efficient memory management and prevents unexpected outcomes when handling strings.
How to remove and replace individual characters or a set of characters
To remove certain characters from a string, you can use str.replace()
or re.sub()
with a regular expression that represents the characters or patterns that you want to remove.
str.replace()
The str.replace()
function is used to remove parts of a text and replace it with a substring.
original_string = "Hello, Python#"
modified_string = original_string.replace('#', '!')
print(original_string) # Output: Hello, Python#
print(modified_string) # Output: Hello, Python!
PythonIn the example above, the replace()
method swaps out the character “#” in original_string
with an exclamation mark “!”.
re.sub()
The re.sub()
function belongs to the re module in Python. With this function, you can search for regular expressions in strings and replace them with other characters.
import re
original_string = "Hello, World! @#$%^&*"
modified_string = re.sub(r'[@#$%^&*]', '', original_string)
print(original_string) # Output: Hello, World! @#$%^&*
print(modified_string) # Output: Hello, World!
PythonThe pattern [@#$%^&*]
is a regular expression that matches the special characters @, #, $, %, ^, &,*
. The re.sub()
function searches for all matches of the pattern in the original string original_string
and replaces them with an empty string ''
. In the example above, we saved the result in the variable modified_string
and output it.
How to remove all characters except letters
You can use different methods to remove all the letters from a string. Below we’ll take a look at how to do this with the following methods: isalpha()
, filter()
and re.sub()
.
re.sub()
import re
original_string = "Hello, 123 World! @#$%^&*"
modified_string = re.sub(r'[^a-zA-Z]', '', original_string)
print(original_string) # Output: Hello, 123 World! @#$%^&*
print(modified_string) # Output: HelloWorld
PythonThe regular expression [^a-zA-Z]
matches any character that is not a lowercase or uppercase letter. As a result, modified_string
is only made up of the letters from the original string. Keep in mind that this removes the spaces between the letters as well.
isalpha()
original_string = "Hello, 123 World! @#$%^&*"
modified_string = ''.join(char for char in original_string if char.isalpha())
print(original_string) # Output: Hello, 123 World! @#$%^&*
print(modified_string) # Output: HelloWorld
PythonIn this example, we used a list comprehension to iterate through each letter in original_string
. The isalpha()
method checks whether a character is a letter. The letters are then inserted into a new modified_string
, while all other characters are ignored.
filter()
original_string = "Hello, 123 World! @#$%^&*"
filtered_chars = filter(str.isalpha, original_string)
modified_string = ''.join(filtered_chars)
print(original_string) # Output: Hello, 123 World! @#$%^&*
print(modified_string) # Output: HelloWorld
PythonThe str.isalpha()
method returns True
if the character is a letter, otherwise it returns False
. The filter()
function creates a filter object containing only the characters that satisfy the str.isalpha()
condition. This allows us to remove all the characters in original_string
that aren’t letters.
How to remove all characters except numbers
As in the previous examples, you can separate numbers using re.sub()
, filter()
and the numerical equivalent to isalpha()
, isdigit()
.
re.sub()
import re
original_string = "Hello, 123 World! @#$%^&*"
modified_string = re.sub('[^0-9]', '', original_string)
print(original_string) # Output: Hello, 123 World! @#$%^&*
print(modified_string) # Output: 123
PythonWith 0-9
, we can define a range that encompasses all digits from 0 to 9. The hyphen -
between 0 and 9 acts as a range operator. The preceding ^
means all characters not within the specified range from 0 to 9. These characters are replaced by empty characters using re.sub
.
filter()
original_string = "Hello, 123 World! @#$%^&*"
filtered_chars = filter(str.isdigit, original_string)
modified_string = ''.join(filtered_chars)
print(original_string) # Output: Hello, 123 World! @#$%^&*
print(modified_string) # Output: 123
PythonThe filter()
function in combination with isdigit()
can filter numbers from a string and remove the remaining characters. Using these together, the new string shows us the digits 123
.
isdigit()
original_string = "Hello, 123 World"
modified_string = ''.join('*' if not char.isdigit() else char for char in original_string)
print(original_string) # Output: Hello, 123 World
print(modified_string) # Output: *******123******
PythonWe can also use the isdigit()
method in a list comprehension to iterate through each char
in original_string
. Characters that are not decimal digits (not char.isdigit()
is true) will be replaced with an asterisk *
, otherwise they stay the same.
How to remove characters using translate()
The translate()
method is a built-in function in Python used for advanced character replacement and translation in strings. It provides an efficient way to replace characters using a table of translations.
original_string = "Hello, World! Remove vowels."
translation_table = str.maketrans(dict.fromkeys('aeiouAEIOU', '*'))
modified_string = original_string.translate(translation_table)
print(original_string) # Output: Hello, World! Remove vowels.
print(modified_string) # Output: H*ll*, W*rld! R*m*v* v*w*ls.
PythonIn the example above, we used the str.maketrans()
constructor and dict.fromkeys()
to create the translation table. This specifies that all vowels are to be replaced by *
. The table is then applied to the original string to get modified_string
.
When filtering strings, you can use the Python string index to filter characters in certain places. To format strings in a specific manner, take a look at our article on Python string formats.
- 99.9% uptime
- PHP 8.3 with JIT compiler
- SSL, DDoS protection, and backups