Uppercasing and lowercasing in Python PREMIUM

Series: Strings
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
3 min. read Watch as video Python 3.10—3.14
Python Morsels
Watch as video
03:17

How can you change the capitalization of a string in Python?

Uppercasing a string in Python

Strings have methods, and many string methods return a new string that's a modification of the original string.

>>> word = "Python"

For example, the upper method will uppercase every letter in a string:

>>> word.upper()
'PYTHON'

Keep in mind that the upper method doesn't modify the string that it's called on. Instead, it returns a new string:

>>> word
'Python'

So to use the return value of upper multiple times, we could point a variable name to it:

>>> loud_word = word.upper()
>>> loud_word
'PYTHON'

Lowercasing a string in Python

What if we wanted to lowercase a string?

Well, strings also have a lower method.

>>> word.lower()
'python'

Just like the upper method, the lower method returns a new string to us. It doesn't modify the original string:

>>> word
'Python'

Case-normalizing in Python

In addition to the upper and lower methods, strings also have a casefold method:

>>> word = "Python"
>>> word.casefold()
'python'

It might seem like casefold does the same thing as lower. And it nearly does.

The only difference between casefold and lower is that casefold normalizes a very small handful of non-English characters that don't have a simple lowercase equivalent.

The classic example, and one of the few useful examples, is the German letter ß, which represents double s:

>>> street = "Straße"

The lower method doesn't do anything to this character:

>>> street.lower()
'straße'

But the casefold method breaks it into two lowercase s characters:

>>> street.casefold()
'strasse'

Some folks prefer to use casefold when normalizing cases, simply for the sake of comparison with another string:

>>> line1 = "I like Python"
>>> line2 = "I like python"
>>> line1.casefold() == line2.casefold()
True

In this case, it doesn't matter whether we're uppercasing or lowercasing our strings. We just need to perform the same operation on both strings before we compare them.

Normalization is the goal, and the casefold method conveys that fact.

But if you find the casefold method confusing, you don't have to use it. We could have used upper or lower here instead.

Capitalizing the first letter

Strings have three other case-modification methods. But they're not nearly as useful as upper, lower, and casefold.

The capitalize method will capitalize the first character of a given string:

>>> name = "python"
>>> name.capitalize()
'Python'

But keep in mind that capitalize does not capitalize the first letter of each word in a string. It just capitalizes the first character.

So if there are multiple words, only the first one will be capitalized:

>>> name = "python software foundation"
>>> name.capitalize()
'Python software foundation'

And if the first character is a quotation mark or some other non-letter, then nothing will be capitalized:

>>> statement = '"hiya" is how I prefer to greet folks'
>>> statement.capitalize()
'"hiya" is how i prefer to greet folks'

Title-casing

If you wanted to capitalize the first letter of every word, you could use the title method for that:

>>> name = "python software foundation"
>>> name.title()
'Python Software Foundation'

Except, the title method isn't without problems.

For example, if you title-case a hyphenated word, the letter just after the hyphen will be capitalized, which may not be desirable:

>>> heading = "The built-in len function"
>>> heading.title()
'The Built-In Len Function'

Even worse, if you title-case a string that includes a contraction, the first letter after the apostrophe will also be capitalized:

>>> subheading = "Python's standard library"
>>> subheading.title()
"Python'S Standard Library"

The title method is very naive with how it decides what constitutes the start of a word, so I would avoid it entirely. See title-case a string in Python for more on this.

Swapping cases

The last case-modification method is the swapcase method, which swaps the case of every letter.

All the uppercase letters become lowercase, and all the lowercase letters become uppercase:

>>> name = "Python Software Foundation"
>>> name.swapcase()
'pYTHON sOFTWARE fOUNDATION'

You will probably never need this method.

Stick with uppercasing, lowercasing, and case-folding

The three useful case-modification methods on Python strings are lower, upper, and casefold.

If you need title-casing or something else, I would look up a solution online instead of using the corresponding string method.

🚀
New to Python? Try Python Jumpstart!

Python Jumpstart is designed to help new Python programmers get up to speed quickly. Get hands-on practice with 50 bite-sized modules that build your Python skills step by step.

Python Morsels
Watch as video
03:17
This is a free preview of a premium screencast. You have 2 previews remaining.