pythonstringmethodscase-sensitivetitle-case

Any difference between str.capitalize() and str.title()?


Is there any difference in str.title() vs str.capitalize()? My understanding from the docs is that both methods capitalize the first letter of a word and make the rest of the letters lowercase. Did anyone run into a situation where they cannot be used interchangeably?


Solution

  • title() changes every word, but capitalize() only the first word in a sentence:

    >>> a = 'nice question'
    >>> a.title()
    'Nice Question'
    >>> a.capitalize()
    'Nice question'
    >>>