how to change the case of first letter of a string? how to change the case of first letter of a string? python python

how to change the case of first letter of a string?


Both .capitalize() and .title(), changes the other letters in the string to lower case.

Here is a simple function that only changes the first letter to upper case, and leaves the rest unchanged.

def upcase_first_letter(s):    return s[0].upper() + s[1:]


You can use the capitalize() method:

s = ['my', 'name']s = [item.capitalize() for item in s]print s  # print(s) in Python 3

This will print:

['My', 'Name']


You can use 'my'.title() which will return 'My'.

To get over the complete list, simply map over it like this:

>>> map(lambda x: x.title(), s)['My', 'Name']

Actually, .title() makes all words start with uppercase. If you want to strictly limit it the first letter, use capitalize() instead. (This makes a difference for example in 'this word' being changed to either This Word or This word)