60

Does anyone know of a really simple way of capitalizing just the first letter of a string, regardless of the capitalization of the rest of the string?

For example:

asimpletest -> Asimpletest
aSimpleTest -> ASimpleTest

I would like to be able to do all string lengths as well.

poke
  • 369,085
  • 72
  • 557
  • 602
Dan
  • 33,953
  • 24
  • 61
  • 87

9 Answers9

142
>>> b = "my name"
>>> b.capitalize()
'My name'
>>> b.title()
'My Name'
tigeronk2
  • 2,494
  • 2
  • 20
  • 21
  • 5
    Using the standard library is def the way to go. – Damian Jul 03 '13 at 10:35
  • 5
    ... except capitalize() removes any other existing caps, which the questioner explicitly didn't want to happen: 'aSimpleTest'.capitalize() => 'Asimpletest' – pfctdayelise Aug 06 '13 at 02:04
  • 2
    @pfctdayelise .. are you down voting all answers not satisfying this requirement or just mine :) ? Though, I agree with your observation. Blair Conrad's answer is most precise. But, other answers add value in presenting useful information around string capitalization, including mine. Three of them were added later than the accepted answer. – tigeronk2 Aug 06 '13 at 12:07
  • 3
    @tigeronk2 I would call it "misleading" rather than "adding value", when the answer doesn't meet the requirements explicitly stated in the question, especially when you don't point out the limitations of your approach. – pfctdayelise Aug 20 '13 at 03:54
77

@saua is right, and

s = s[:1].upper() + s[1:]

will work for any string.

ayhan
  • 70,170
  • 20
  • 182
  • 203
Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
12

What about your_string.title()?

e.g. "banana".title() -> Banana

skyler
  • 8,010
  • 15
  • 46
  • 69
  • 4
    `str.title` capitalizes each _word_ in the string – John La Rooy Feb 03 '14 at 04:18
  • @John La Rooy ...which is exactly what I wanted to do without writing a new def to split, capitalize each word, and put it back together... upvote to skyler – me_ Feb 02 '18 at 17:12
8
s = s[0].upper() + s[1:]

This should work with every string, except for the empty string (when s="").

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
5

this actually gives you a capitalized word, instead of just capitalizing the first letter

cApItAlIzE -> Capitalize

def capitalize(str): 
    return str[:1].upper() + str[1:].lower().......
Community
  • 1
  • 1
rbp
  • 1,850
  • 15
  • 28
3

for capitalize first word;

a="asimpletest"
print a.capitalize()

for make all the string uppercase use the following tip;

print a.upper()

this is the easy one i think.

faiz
  • 115
  • 1
  • 10
2

You can use the str.capitalize() function to do that

In [1]: x = "hello"

In [2]: x.capitalize()
Out[2]: 'Hello'

Hope it helps.

Eric
  • 2,636
  • 21
  • 25
2

Docs can be found here for string functions https://docs.python.org/2.6/library/string.html#string-functions
Below code capitializes first letter with space as a separtor

s="gf12 23sadasd"
print( string.capwords(s, ' ') )

Gf12 23sadasd

Saurabh
  • 7,525
  • 4
  • 45
  • 46
-4
str = str[:].upper()

this is the easiest way to do it in my opinion

saar
  • 1