-3

I have researched the string.upper() command and can't seem to figure out how to print a string where only the first letter is upper cased.

string = 'name'
print string.upper()

print string[0].upper(), string[1:]

Could someone please give me a hint on how to do this?

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
user3264459
  • 15
  • 1
  • 4

2 Answers2

2

str.capitalize does exactly what you need.

capitalize(...)
    S.capitalize() -> string

    Return a copy of the string S with only its first character
    capitalized.

Related is

title(...)
    S.title() -> string

    Return a titlecased version of S, i.e. words start with uppercase
    characters, all remaining cased characters have lowercase.

Note that capitalize makes all the following letters lowercase. ie equivalent to
string[0].upper() + string[1:].lower().
So if you need to preserve the case of those, you'll need to stick to your original solution

>>> "fOO".capitalize()
'Foo'
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
1

There is a string method capitalize for this purpose.

>>> string = "hello world"
>>> string.capitalize()
'Hello world'
Sunny Nanda
  • 2,362
  • 1
  • 16
  • 10
  • Duplicate of the accepted answer.... And also not as elaborated as @JohnLaRooy's answer. – dapc Sep 23 '19 at 10:07