In this lesson, you’ll explore string methods that perform case conversion on the target string. These are the case conversion methods:
str.capitalize()
str.lower()
str.swapcase()
str.title()
str.upper()
Here’s how to use str.capitalize()
:
>>> s = 'egG BacOn SauSAGE loBSter'
>>> s.capitalize()
'Egg bacon sausage lobster'
>>> s = 'egg123#BACON#.'
>>> s.capitalize()
'Egg123#bacon#.'
Here’s how to use str.lower()
:
>>> s = 'EGG Bacon 123 sAusAge lOBSTEr.'
>>> s.lower()
'egg bacon 123 sausage lobster.'
Here’s how to use str.swapcase()
:
>>> s = 'eGG Bacon 123 sausage LOBSTER.'
>>> s.swapcase()
'Egg bACON 123 SAUSAGE lobster.'
Here’s how to use str.title()
:
>>> s = 'the sun also rises'
>>> s.title()
'The Sun Also Rises'
>>> s = "what's happened to ted's IBM stock?"
>>> s.title
"What'S Happened To Ted'S Ibm Stock?"
Here’s how to use str.upper()
:
>>> s = 'EGG Bacon 123 sauSAGE lOBsTer.'
>>> s.upper()
'EGG BACON 123 SAUSAGE LOBSTER.'