tags:

views:

59

answers:

1

I'm using the re library to normalize some text. One of the things I want to do is replace all uppercase letters in a string with their lower case equivalents. What is the easiest way to do this?

+7  A: 
>>> s = "AbcD"
>>> s.lower()
'abcd'

There is also a swapcase method if you want that.

See: http://docs.python.org/library/stdtypes.html#string-methods

Nux
thanks. maybe you should edit it to str = str.lower() though.
alphomega
Depends on what you want to do. If you want to change the original string, then of course you're right - str.lower() alone does not change the original string.
Nux
@Nux: He's right that your example is confusing. Your last line was 'str' (bad example variable choice, given the builtin str type), and the result of that line is *not* 'abcd'. I've edited it to a simple python repl session instead.
Roger Pate
@Roger Pate: OK, using `str` might be confusing, but still it would work and the result of `str.lower()` in my original example *is* 'abcd'.
Nux
@Nux: Yes, but your original last line, right above where you gave the result, was str, *not* str.lower(). That's all I was pointing out and what I believe alphomega was pointing out.
Roger Pate
Oh, that's what you meant. OK, my bad :-). Though the console would show one what I meant ;-).
Nux