0
0
Swiftprogramming~5 mins

Character and String types in Swift

Choose your learning style9 modes available
Introduction

Characters and strings let us work with letters, words, and sentences in our programs. They help us handle text like names, messages, or any written information.

When you want to store a single letter or symbol, like a grade 'A' or a punctuation mark.
When you need to keep a full word or sentence, like a user's name or a greeting message.
When you want to check or change parts of a text, like finding the first letter of a word.
When you want to combine words to make sentences, like joining "Hello" and "World".
Syntax
Swift
let letter: Character = "A"
let word: String = "Hello"

A Character holds exactly one letter or symbol.

A String holds zero or more characters, like words or sentences.

Examples
This creates a single character 'Z'.
Swift
let letter: Character = "Z"
This creates a string with multiple characters forming a greeting.
Swift
let greeting: String = "Hi there!"
Characters can also be emojis or special symbols.
Swift
let emoji: Character = "😊"
This creates an empty string with no characters.
Swift
let emptyString: String = ""
Sample Program

This program shows how to create a character and a string, then combines them to make a sentence.

Swift
import Foundation

let firstLetter: Character = "S"
let name: String = "Swift"

print("First letter: \(firstLetter)")
print("Name: \(name)")

// Combine characters to make a string
let combined = String(firstLetter) + " is for " + name
print(combined)
OutputSuccess
Important Notes

Use double quotes "" for both characters and strings in Swift.

Characters must hold exactly one symbol; strings can hold many.

You can convert a Character to a String using String(character).

Summary

Character stores one letter or symbol.

String stores many characters, like words or sentences.

Use them to work with text in your programs.