Chapter 20: Swift Characters
1. What is a Character in Swift?
A Character represents a single user-perceived character — it is the smallest unit of text that a person would consider “one character”.
Important things to understand right from the start:
- In Swift, a Character is not the same as a byte or a Unicode scalar.
- A single Character can be:
- one basic letter (“A”, “z”, “5”, “₹”)
- an emoji (“😊”, “🚀”, “🇮🇳”)
- a letter + combining mark (“é”, “ā”, “न”)
- a flag emoji made from two regional indicators (“🇮🇳”)
Key rule (very important):
In almost all real-world code, you will use String even when you only have one character. Character is used only when you really need to work with individual grapheme clusters (human-visible characters).
2. How to create a Character
There are two common ways:
Way 1 – Literal character (most common)
|
0 1 2 3 4 5 6 7 8 9 10 |
let letterA: Character = "A" let rupee: Character = "₹" let smile: Character = "😊" let flagIndia: Character = "🇮🇳" let combined: Character = "é" // e + combining acute accent |
Notice: even though you write “A”, because we explicitly say : Character, Swift treats it as one Character.
Way 2 – From a String (very common in real code)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
let name = "Priya" // First character of the string let firstChar = name.first! // Character? (optional because string can be empty) // Last character let lastChar = name.last! // Character? // Or safely if let first = name.first { print("First character is: \(first)") } |
3. Why Character is different from what you might expect
In many older languages (C, C++, Java), a “char” is just one byte or one Unicode code point.
Swift is different — it follows modern Unicode rules.
Example that surprises many people:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
let e1: Character = "e" let e2: Character = "é" // e + combining acute accent let e3: Character = "é" // same as above — one Character print(e1 == e2) // false print(e2 == e3) // true ← Swift sees them as the same character print(e2.count) // wait — count is for String, not Character |
4. Character vs String – very important comparison
| Feature / Question | Character | String |
|---|---|---|
| How many characters? | Exactly one visible character | Zero or more |
| Can hold emoji? | Yes | Yes |
| Can hold combined characters? | Yes (one complete unit) | Yes |
| Can be empty? | No | Yes (“”) |
| Has .count property? | No | Yes |
| Can be concatenated with +? | No | Yes |
| Most common in real code? | Rarely | Very often |
| Type when you write “A” | String (unless you specify : Character) | String |
Very common real-life pattern:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
let text = "Hello, Hyderabad! 😊" // Almost always you treat it as String print(text.count) // 18 (including emoji and spaces) // But sometimes you want individual characters for char in text { print(char) // H, e, l, l, o, ,, space, H, y, d, e, r, a, b, a, d, !, space, 😊 } |
Notice: when you loop over a String, each item you get is a Character!
5. Real-life examples – when people actually use Character
Example 1 – Checking first/last letter
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
func isNameStartingWithVowel(_ name: String) -> Bool { guard let first = name.first else { return false } let vowels: Set<Character> = ["A", "E", "I", "O", "U", "a", "e", "i", "o", "u"] return vowels.contains(first) } print(isNameStartingWithVowel("Aarav")) // true print(isNameStartingWithVowel("Rahul")) // false |
Example 2 – Formatting phone number with separators
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
func formatIndianPhoneNumber(_ number: String) -> String { var result = "" var index = 0 for char in number { if index == 0 { result += "+91 " } else if index == 5 { result += " " } result.append(char) index += 1 } return result } print(formatIndianPhoneNumber("9876543210")) // Output: +91 98765 43210 |
Example 3 – Password strength – checking character types
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
func passwordStrength(_ password: String) -> String { var hasUpper = false var hasLower = false var hasDigit = false var hasSpecial = false let specialChars: Set<Character> = ["!", "@", "#", "$", "%", "^", "&", "*"] for char in password { if char.isUppercase { hasUpper = true } if char.isLowercase { hasLower = true } if char.isNumber { hasDigit = true } if specialChars.contains(char) { hasSpecial = true } } var score = 0 if hasUpper { score += 1 } if hasLower { score += 1 } if hasDigit { score += 1 } if hasSpecial { score += 1 } switch score { case 4: return "Very Strong" case 3: return "Strong" case 2: return "Medium" default: return "Weak" } } print(passwordStrength("Pass123!")) // Strong print(passwordStrength("abc123")) // Medium |
6. Very common beginner mistakes
| Mistake | Wrong code | Correct way | Why? |
|---|---|---|---|
| Declaring single letters as Character everywhere | let grade: Character = “A” | let grade = “A” | String is usually enough and more flexible |
| Thinking Character is always one byte | Expecting “😊”.count == 1 as Character | Loop over String → each item is Character | Emoji and combined chars can be multiple scalars |
| Using == between Character and String | “A” == “A” (works) but careful | Prefer Character vs Character comparison | Type safety |
| Forgetting strings can be empty | string.first! without check | string.first (optional) | Avoid crashes |
7. Quick reference – Character vs String cheat sheet
| Goal | Use Character or String? | Example code |
|---|---|---|
| Store one visible character | Character | let symbol: Character = “₹” |
| Store text (even one letter) | String (preferred) | let initial = “A” |
| Loop over each visible character | Loop over String | for char in text { … } — each char is Character |
| Check if a letter is uppercase | Character property | char.isUppercase |
| Check if a symbol is emoji | Character property | char.isEmoji |
| Get first/last character of a string | String.first / String.last | name.first! |
8. Summary – when should you use Character?
Use Character when:
- You are explicitly working with individual visible characters (looping over a string)
- You need properties like .isUppercase, .isNumber, .isEmoji, .isLetter
- You are building or parsing something character-by-character (phone number formatting, password validation)
Use String almost always when:
- You just need to store or display text — even if it’s one character
- You want to concatenate, count characters, search, replace, etc.
Which part would you like to explore more deeply next?
- Properties of Character (isUppercase, isNumber, isEmoji, isWhitespace…)
- How Characters work with Unicode (very important topic)
- Working with strings character by character
- Or move to another topic (strings in detail, arrays, optionals…)
Just tell me — we’ll continue in the same detailed, patient, teacher-like way 😊
