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
2010-10-19 06:41:43
thanks. maybe you should edit it to str = str.lower() though.
alphomega
2010-10-19 07:07:01
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
2010-10-19 07:16:18
@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
2010-10-19 07:30:09
@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
2010-10-19 07:58:09
@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
2010-10-19 08:05:07
Oh, that's what you meant. OK, my bad :-). Though the console would show one what I meant ;-).
Nux
2010-10-19 21:14:52