Start learning free

LESSON 66 OF 415 · STRINGS I

Uppercase and Lowercase in Swift: uppercased() and lowercased()

Lesson Read the full explanation

In Swift, String values have built-in methods to change their case:

s.uppercased()   // converts all characters to uppercase
s.lowercased()   // converts all characters to lowercase

Here are some examples:

let word = "Hello World"
let upper = word.uppercased()  // "HELLO WORLD"
let lower = word.lowercased()  // "hello world"

let name = "ALICE"
print(name.lowercased())  // "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.uppercased() and word.lowercased().

STEP 1 OF 2
    Ctrl + Enter to check

    SWIFT · STRINGS I

    String Upper Lower in Swift, explained

    Changing the case of a string

    Swift strings have two built-in methods for case: uppercased() and lowercased(). They need no import. Each one converts every letter and leaves other characters, such as digits, spaces and punctuation, as they are.

    let word = "Hello World"
    let upper = word.uppercased()
    let lower = word.lowercased()
    
    print(upper)
    print(lower)
    print(word)
    
    HELLO WORLD
    hello world
    Hello World
    

    Neither method changes word. Both return a new string, as the last line shows. The -ed ending is Swift's naming convention: methods named like this (uppercased, sorted, reversed) return a new value and leave the original alone.

    Comparing without caring about case

    Strings are compared character by character, so "Hello" and "hello" are different. A common trick is to convert both sides to the same case first:

    let a = "Hello"
    let b = "hello"
    print(a == b)
    print(a.lowercased() == b.lowercased())
    
    false
    true
    

    This works for user input such as a typed answer or a command, where capital letters shouldn't change the meaning.

    Capitalizing the first letter

    There is no built-in method that capitalizes only the first letter. Combine prefix, dropFirst and uppercased():

    let text = "hello world"
    let capitalized = text.prefix(1).uppercased() + text.dropFirst()
    print(capitalized)
    
    Hello world
    

    prefix(1) is the first character as a Substring, and uppercased() turns it into a String. Adding the rest of the text with + produces a new string.

    Checking the case

    To ask about the case of existing text, use the isUppercase and isLowercase properties of a Character. A string is already all uppercase when it equals its own uppercased version:

    let word = "Hello World"
    print(word.first!.isUppercase, word.last!.isUppercase)
    print(word.filter { $0.isUppercase }.count)
    print(word == word.uppercased())
    
    true false
    2
    false
    

    The second line counts the capital letters by keeping only the uppercase characters and measuring what is left.

    Details worth knowing

    • The methods follow Unicode rules, so accented letters convert too: "ÉCOLE".lowercased() gives école.
    • The result can be longer than the original. "straße".uppercased() is STRASSE, because the uppercase form of ß is two letters.
    • A single Character has the same methods, but uppercased() on a Character returns a String, since the result may be more than one character.

    Chaining with other methods

    Since each call returns a string, case changes combine with other string methods. For example, " Hello ".lowercased().count is 9: lowercasing doesn't change the length of this text, and the spaces are still counted. Convert the case first or last depending on what the next step needs, but remember that each call creates a fresh string and leaves the one before it unchanged.

    The exercise

    The constant word holds "Hello World". You add two constants, one per step. Declare upper with let and assign it word.uppercased(). Then declare lower in the same way with word.lowercased(). The two print calls in the starter code then show them.

            let word = "Hello World"
    
            let upper = word.uppercased()
            let lower = word.lowercased()
            print(upper)
            print(lower)
    
    HELLO WORLD
    hello world
    

    Common mistakes

    • Calling the method and discarding the result. Writing word.uppercased() on its own line changes nothing; the new string must be stored or printed.
    • Forgetting the parentheses. uppercased without () is the method itself, not its result.
    • Using the wrong spelling. The methods are uppercased() and lowercased(), with the -ed.
    • Using let for something you want to reassign. To replace the original variable, declare it with var and write word = word.uppercased().

    Key takeaways

    • uppercased() and lowercased() return a new string and leave the original unchanged.
    • Convert both sides to one case to compare strings without regard to case.
    • Only letters change. Digits, spaces and punctuation stay the same.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free