DEV Community

Cover image for Python Utility Belt: Replacing differents chars in a String.
Leonardo Furtado
Leonardo Furtado

Posted on

Python Utility Belt: Replacing differents chars in a String.

Hi Devs,
This is my first post on, and i decided talk to about Python, my main programming language, and create a series named 'Python Utility Belt' which basically will be composed of small strategies to solve common problems in a development day.
In this post we will talk to about chars replacement.

So, lets imagine a simple example... you have the following string:

music = "Stairway_to_heaven-Led_Zeppelin.mp3"
Enter fullscreen mode Exit fullscreen mode

And you need to replace "_" with spaces, in Python it is easy, you just need run:

music.replace('_',' ')
Enter fullscreen mode Exit fullscreen mode

and you will receive:

Stairway to heaven-Led Zeppelin.mp3
Enter fullscreen mode Exit fullscreen mode

Its so easy, isn't it?! But, what if you desire to replace _,- and . with spaces ???
A python beginner can imagine a code like this:

music.replace('_',' ').replace('-',' ').replace('.',' ')

Enter fullscreen mode Exit fullscreen mode

yeah... And it is a common answer in stackoverflow. Does the code above works? yep, it works, but there is another way to solve the same problem: using re lib. You just need to import re in you code and use the sub function:

import re

music = "Stairway_to_heaven-Led_Zeppelin.mp3"

print(re.sub('_|-|\.', ' ', music))
Enter fullscreen mode Exit fullscreen mode

In this example, we called the sub function that receives three parameters:

re.sub(char that will be replaced,new char,string)
Enter fullscreen mode Exit fullscreen mode

to replace multiple chars using the first parameter we used regex, but we will not delve into it, no. Basically each char that need to be replaced is separated by |, so if you need to replace just _ and - you can run re.sub('_|-', ' ', music). Note that . is accompanied by \, it is because in Regex, . has a another functionality, so we need to use \ to specify the use of a dot.

I will finalize this post here.
Please, comment if you found any error on post (English, programming, syntax).
This helps me to improve.

Follow me for more, bye.

Top comments (3)

rpalo profile image
Ryan Palo

Hi! Thanks so much for sharing! For me, it was really scary posting my first time, so I know it took some extra courage to post here.

This is a great post! I always forget about the other little helper methods in re besides plain match.

Did you know that if you add a language name after the first three ticks of the code fence (‘’’python), you’ll get syntax highlighting?

Your English looks good! I didn’t read through with a fine tooth comb, but there were no mistakes that were distracting. In the future, feel free to message me here on if you’d like a second set of eyes to do some extra English proofreading 😁 or if you have any questions or need anything else!

furtleo profile image
Leonardo Furtado

Hi Ryan, thanks for you reply.

I will improve my knowledge on the platform to make the next posts, I did not know about the syntax highlighting here. Thank you again.

pragmaticpi profile image
Piyush Kumar • Edited

Good one. In my opinion at the end of the day, you will use replace method only. Re lib is useful if you have some string to be validated first and then replaced.