Search code examples
pythonstringreplacelowercase

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?


Solution

  • 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.