Changing the first letter of a string into upper case in python

Viewed 12488

I aim to convert a proper noun for instance to have an upper case first letter after an input of the name has been made.

4 Answers

Using string.title() you achieve that:

>>> name = 'joe'
>>> name.title()
'Joe'

Use upper() method, like this:

mystr = "hello world"
mystr = mystr[0].upper() + mystr[1:]

.capitalize() and .title(), can be used, but both have issues:

>>> "onE".capitalize()
'One'
>>> "onE".title()
'One'

Both changes other letters of the string to smallcase. Write your own:

>>> xzy = lambda x: x[0].upper() + x[1:]
>>> xzy('onE')
'OnE'
Related