Start learning free

LESSON 69 OF 344 · STRINGS I

String Upper and Lower in Python: upper() and lower()

Lesson Read the full explanation

In Python, strings have built-in methods to change their case:

s.upper()   # converts all characters to uppercase
s.lower()   # converts all characters to lowercase

Here are some examples:

word = "Hello World"
print(word.upper())  # "HELLO WORLD"
print(word.lower())  # "hello world"

name = "ALICE"
print(name.lower())  # "alice"

These methods return a new string — the original is not modified.


Assignment
Follow the Coding Tutorial and convert word to both uppercase and lowercase!


Hint
Use word.upper() and word.lower().

STEP 1 OF 2
    Ctrl + Enter to check

    PYTHON · STRINGS I

    String Upper Lower in Python, explained

    What upper() and lower() do

    Python strings have two methods for changing letter case. upper() returns the string with every letter in uppercase, and lower() returns it with every letter in lowercase. Characters that are not letters, such as digits, spaces and punctuation, pass through unchanged.

    word = "Hello World"
    print(word.upper())  # HELLO WORLD
    print(word.lower())  # hello world
    print(word)          # Hello World
    
    name = "ALICE"
    print(name.lower())  # alice
    print("abc123".upper())  # ABC123
    

    The third print shows that word itself is still Hello World. Strings are immutable in Python, which means a string cannot be modified after it is created. A method like upper() builds a new string and returns it, and the original is left alone. If you want to keep the result, you have to store it in a variable.

    Why case conversion is useful

    Python compares strings character by character, and an uppercase letter is a different character from its lowercase partner. So these two strings are not equal:

    print("Yes" == "yes")                  # False
    print("Yes".lower() == "yes")          # True
    print("Ab".lower() == "aB".lower())    # True
    

    Converting both sides to the same case is the standard way to compare text while ignoring case, for instance when checking an answer typed by a user. Converting for display is the other common use: upper() for a heading or a code, lower() for normalising input before storing it.

    Case methods combine well with other string methods, because each one returns a string. A typed answer with extra spaces and a different case can be cleaned and checked in one expression:

    answer = "  YES \n"
    if answer.strip().lower() == "yes":
        print("confirmed")
    

    This prints confirmed. The call strip() removes the surrounding whitespace, lower() lowers the letters, and only then does the comparison happen.

    Related methods

    A few more case methods are worth knowing:

    • capitalize() makes the first character uppercase and the rest lowercase: "Hello World".capitalize() gives Hello world.
    • title() capitalises each word: "hello world".title() gives Hello World. It treats an apostrophe as a word boundary, so "they're".title() gives They'Re.
    • swapcase() flips the case of every letter.
    • isupper() and islower() do not convert anything. They answer whether the letters in a string are all uppercase or all lowercase, returning True or False.

    For comparing text that may contain letters from many languages, casefold() is a stronger version of lower(). For example, "straße".casefold() gives strasse. For ordinary English text, lower() is enough.

    Common mistakes

    • Ignoring the result. A line like word.upper() on its own does nothing visible. The uppercase string is created and discarded, and word keeps its old value. Write word = word.upper() or assign to a new name.
    • Forgetting the parentheses. word.upper is the method itself, not a converted string. Printing it shows something like a bound method, not HELLO WORLD.
    • Expecting digits to change. Numbers and symbols have no case. "123".isupper() is False, because the string has no uppercase letters at all.
    • Comparing without converting. "Yes" == "yes" is False. Convert both sides first.

    The exercise, step by step

    The lesson gives word set to "Hello World" and asks for two new variables, one in each case.

    1. Create the uppercase version. Call word.upper() and assign the result to a variable named upper. It holds "HELLO WORLD".
    2. Create the lowercase version. Call word.lower() and assign the result to a variable named lower. It holds "hello world".
    word = "Hello World"
    
    upper = word.upper()
    lower = word.lower()
    

    Both calls start from word, and neither changes it. That is why the second line can still use word and get the original mixed-case text: the first line did not modify it.

    The names upper and lower are the same as the method names, which is fine. A variable name is just a label, and word.upper() is looked up on the string, not among your variables. The two do not interfere.

    Key takeaways

    • s.upper() and s.lower() return a new string in uppercase or lowercase.
    • Only letters change. Digits, spaces and punctuation stay as they are.
    • The original string is never modified, so store the result.
    • Convert both strings to the same case before comparing text without regard to case.
    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free