Remove the hyphens from a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 14, 2022

banner

Photo from Unsplash

Remove the hyphens from a String in Python #

Use the str.replace() method to remove the hyphens from a string, e.g. result = my_str.replace('-', ''). The replace() method will remove the hyphens from the string by replacing them with empty strings.

main.py
my_str = "ab-cd-ef-gh" # ✅ remove the hyphens from a string result = my_str.replace('-', '') print(result) # 👉️ 'abcdefgh' # ----------------------------------------- # ✅ remove the hyphens from a list of strings my_list = [ '12-34-', '56-78-', '90-12-', '34-56-' ] result = [item.replace('-', '') for item in my_list] print(result) # 👉️ ['1234', '5678', '9012', '3456']

We used the str.replace() method to remove the hyphens from a string.

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)

The method doesn't change the original string. Strings are immutable in Python.

We remove all hyphens from the string by replacing them with empty strings.

main.py
my_str = "ab-cd-ef-gh" result = my_str.replace('-', '') print(result) # 👉️ 'abcdefgh'

If you need to remove all hyphens from a list of strings, use a list comprehension.

main.py
my_list = [ '12-34-', '56-78-', '90-12-', '34-56-' ] result = [item.replace('-', '') for item in my_list] print(result) # 👉️ ['1234', '5678', '9012', '3456']

We used a list comprehension to iterate over the list of strings.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the replace() method to remove all hyphens from the current string and return the result.

The strings in the new list don't contain any hyphens.

An alternative way to remove all hyphens from a string is to use a generator expression.

main.py
my_str = "ab-cd-ef-gh" result = ''.join(char for char in my_str if char != '-') print(result) # 👉️ 'abcdefgh'

We used a generator expression to iterate over the string.

On each iteration, we check if the current character is not equal to a hyphen and return the result.

The output is a generator object that contains the characters of the string excluding the hyphens.

The last step is to use the str.join() method to join the characters into a string.

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

main.py
my_str = "ab-cd-ef-gh" result = ''.join(char for char in my_str if char != '-') print(result) # 👉️ 'abcdefgh'

For our purposes, we called the str.join() method on an empty string to join the remaining characters without a separator.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.