Python @ DjangoSpin

PyPro #70 Replace _ , . with @ in a string

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Replace characters in a string in Python

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.


See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply