Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Case Conversion

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()

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()

00:00 In this video, you’ll look at the first category of string methods. You’ll dive into case conversion. The first method you’re going to try out is .capitalize().

00:09 It’s going to return a copy of the string with the first character converted to uppercase and all the other characters converted to lowercase. Non-alphabetic characters are left unchanged. First off, you’re creating a string with a mix of characters, some uppercase and some lowercase. Now that s is created, it’s a string object, and if you enter s dot (.) if you’re using a tool such as bpython, it suddenly shows you all the methods that are available to use on this object.

00:40 You’re going to cover actually the majority of them in the next set of tutorials. The first one, .capitalize(), it’s going to just make the first character have an uppercase and the rest is lower. All right, try it out. So as you can see here, all the additional uppercase characters, it converted them into lowercase, starting with just one uppercase character.

00:59 That’s .capitalize(). And note: special characters and numbers and punctuation will not be affected. So if you apply .capitalize() to that string, s, you’ll see again, those will remain unchanged. Next up, .lower().

01:19 This method converts all alphabetic characters to lowercase. So here we go again,

01:30 a string with a mix of characters,

01:35 and applying .lower() is going to return a copy of that string converted entirely to lowercase. As you can see, all alphabetic characters—oh, even the misspelled 'bacon'—is returned all lowercase.

01:51 Next, .swapcase(). This one returns a copy of the string with uppercase alphabetic characters converted to lowercase and vice versa. I’ve created a string with a mix of uppercase and lowercase in words, and also entire words that are uppercase and lowercase, along with a couple non-alphabetic characters.

02:12 So, what would .swapcase() do? .swapcase() is going to convert uppercase to lower and lowercase characters to uppercase. So, what does that look like? As you can see here, it just swapped the cases of both. Pretty straightforward.

02:27 The method .title() converts the target string into title case, where the first letter of each word is converted to uppercase and the remaining letters are lowercase. One note: this is a very simple algorithm that it’s using here.

02:40 It does not attempt to distinguish between important and unimportant words, and does not handle apostrophes or possessives or acronyms very gracefully. So, just be aware. It simply capitalizes the first letter of each word.

02:56 Let’s say you had a title—

03:00 in this case, a title of a book.

03:05 It’s going to return a version of the string where each word is title cased. It’s going to start just each word with an uppercase character. All right, nice. And as was mentioned, if you have a more complex string, it may not turn out exactly as you planned. For this one, I’m going to use a couple apostrophes, so I’m starting with using a double quote (") to start the string instead—that way, it won’t close the string object—and also going to include an acronym for "IBM". So, there’s what it looks like currently.

03:38 What will .title() do to this? So again, words start with an uppercase character and all remaining characters have a lowercase. Well, as you can see, it doesn’t know exactly what a word is, what an apostrophe is or what an acronym is. It’s a very simple algorithm, just returning a capital at the front of each word.

03:59 You might need something more advanced than that, depending on what you’re trying to title. So next, .upper() converts all alphabetic characters to uppercase.

04:11 To try this out yourself, create another string with a mix of upper and lowercase.

04:18 So, what will s.upper() return? Copy the string with everything converted to uppercase, all alphabetic characters. Great! After covering all the case conversion methods, now it’s time to do a little bit of find and seek.

Become a Member to join the conversation.