0

I have to capitalize the first character of a word without lowering any letters. I've tried using title() and capitalize() but they change the whole word by lowering the capitalized letter(s).

word = "javaScript language"

I want the output to be JavaScript language.

Norah
  • 33
  • 6

2 Answers2

5
>>> word
'javaScript language'
>>> word[0].upper() + word[1:]
'JavaScript language'
g4ur4v
  • 3,210
  • 5
  • 32
  • 57
3

Another variant using slices the whole way

>>> word = "helloWorld"
>>> word
'helloWorld'
>>> word = word[:1].upper() + word[1:]
>>> word
'HelloWorld'

Per DSM's comment this version has better support for an empty string. calling [0] on an empty string will result in an error. Whereas [:1] and [1:] both return empty strings.

Mike McMahon
  • 7,096
  • 3
  • 30
  • 42
  • 2
    One advantage this has over `word[0]` is that it cleanly handles the empty string. – DSM Oct 01 '14 at 23:17