Reading Time: 1 minutes
Replace characters in a string in Python
Replace _ , . with @ in a string. Write a Regular Expression in Python to replace any occurrence of underscore, comma, or period with an at-the-rate symbol i.e. @.
Replace _ , . with @ in a string
import re sourceString = 'Some string with _ & , & . & , & .' print( re.sub('[_,\.]', '@', sourceString) ) # 'Some string with @ & @ & @ & @ & @'
To learn more about Regular Expressions in Python, click here.