python capitalize first letter only

user1442957 picture user1442957 · Sep 13, 2012 · Viewed 216.3k times · Source

I am aware .capitalize() capitalizes the first letter of a string but what if the first character is a integer?

this

1bob
5sandy

to this

1Bob
5Sandy

Answer

DSM picture DSM · Sep 13, 2012

Only because no one else has mentioned it:

>>> 'bob'.title()
'Bob'
>>> 'sandy'.title()
'Sandy'
>>> '1bob'.title()
'1Bob'
>>> '1sandy'.title()
'1Sandy'

However, this would also give

>>> '1bob sandy'.title()
'1Bob Sandy'
>>> '1JoeBob'.title()
'1Joebob'

i.e. it doesn't just capitalize the first alphabetic character. But then .capitalize() has the same issue, at least in that 'joe Bob'.capitalize() == 'Joe bob', so meh.