Here are five string methods you can use for changing the case of characters. Word level transformation is determined by consecutive occurrences of alphabets, not limited to separation by whitespace characters.

>>> sentence = 'thIs iS a saMple StrIng'

>>> sentence.capitalize()
'This is a sample string'

>>> sentence.title()
'This Is A Sample String'

>>> sentence.lower()
'this is a sample string'

>>> sentence.upper()
'THIS IS A SAMPLE STRING'

>>> sentence.swapcase()
'THiS Is A SAmPLE sTRiNG'

The string.capwords() method is similar to title() but also allows a specific separator (default is whitespace).

>>> import string
>>> phrase = 'this-IS-a:colon:separated,PHRASE'

# every word is transformed
>>> phrase.title()
'This-Is-A:Colon:Separated,Phrase'

# colon character is used as the text boundary
>>> string.capwords(phrase, ':')
'This-is-a:Colon:Separated,phrase'

Video demo:


info See also my 100 Page Python Intro ebook.