Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - replacing lower case letters

>>> import string
>>> word = "hello."
>>> word2 = word.replace(string.lowercase, '.')
>>> print word2
hello.

I just want all the lowercase letters to turn into full stops.

What am I doing wrong here?

like image 331
james_kansas Avatar asked May 01 '26 09:05

james_kansas


1 Answers

Use a regular expression:

from re import sub

print sub("[a-z]", '.', "hello.")

str.replace is looking for the string abcdefghijklmnopqrstuvwxyz to replace it with ., not looking for each individual letter to replace.

like image 65
agf Avatar answered May 02 '26 23:05

agf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!