Swift Your First Program: print("Hello World") Explained

This article teaches you how to write and understand your first Swift program using print("Hello World"). Although it looks simple, this tiny program introduces core Swift ideas such as function calls, strings, console output, and basic program structure. By the end, you will know how it works, how to modify it, how to avoid beginner mistakes, and how to build a small command-line example from it.

1. What Is Your First Swift Program?

Your first Swift program is usually a minimal command-line statement that prints text to the console. In Swift, the classic beginner example is:

print("Hello World")

This single line is a complete Swift statement. It tells Swift to display the text Hello World as output.

In Swift, print is a standard library function. You do not need to import anything to use it in a basic Swift file.

2. Why Your First Swift Program Matters

A first program is not important because of what it does. It matters because of what it teaches. The line print("Hello World") helps you understand how Swift code is written and executed.

It is useful when you want to:

It is not meant for graphical apps, user interfaces, or data processing by itself. It is a starting point. Once you understand this line, you can expand into more practical programs.

3. Basic Syntax or Core Idea

Understanding the minimal syntax

The basic Swift syntax for your first program is shown below. Read it as: call the print function and pass it a string value.

print("Hello World")

Here is what each part means:

How Swift reads this line

Swift evaluates the argument inside the parentheses and then sends it to the print function.

// Swift evaluates the string and prints it
print("Hello World")

The output is:

Hello World

This shows the core pattern you will use throughout Swift: call a function, pass data to it, and get a result or side effect.

Adding more output

You can call print more than once to output multiple lines.

print("Hello World")
print("Welcome to Swift")

This creates two separate lines of output. Each call to print ends with a newline by default.

4. Step-by-Step Examples

Example 1: The classic Hello World

This is the simplest valid Swift example for console output. It prints one fixed string.

print("Hello World")

This is useful when you are testing that Swift runs correctly and when you are learning the shape of a function call.

Example 2: Printing your own message

You are not limited to Hello World. Any string literal can be printed.

print("Hello from Swift 5")

This example shows that the important part is not the exact phrase, but the syntax: a function name, parentheses, and a string in quotes.

Example 3: Printing multiple lines with separate calls

A real program often prints more than one message. The next example uses multiple statements.

print("Starting program...")
print("Hello World")
print("Program finished.")

This demonstrates that a Swift program is usually a sequence of statements executed from top to bottom.

Example 4: Printing a variable

Once you move beyond fixed text, you will often store a value in a variable or constant and then print it. This is a natural next step from your first program.

let message = "Hello World"
print(message)

Here, message stores the string, and print outputs that stored value. This is how simple examples grow into real programs.

Example 5: Combining text with a value using interpolation

Swift lets you insert values into strings using interpolation. This is very common in practical code.

let language = "Swift"
print("Hello from \(language)")

The output includes the value of language. This shows that print is not only for fixed text; it is also used to inspect values while your program runs.

5. Practical Use Cases

Even though print("Hello World") is a learning example, the print function itself is genuinely useful in real Swift development.

In production apps, you may later use more advanced logging tools, but print remains useful for learning, testing, and simple scripts.

6. Common Mistakes

Mistake 1: Forgetting quotation marks around text

Beginners often write plain text without quotes. In Swift, text literals must use double quotes.

Warning: This is invalid because Swift treats unquoted text as identifiers, not a string.

// Bad
print(Hello World)

The correct version wraps the text in double quotes.

// Correct
print("Hello World")

This works because Swift now recognizes the argument as a string literal.

Mistake 2: Using the wrong quotation marks

Text copied from word processors or websites may contain curly quotes instead of normal double quotes.

Warning: Smart quotes are not valid Swift string delimiters.

// Bad
print(“Hello World”)

Use straight double quotes from your code editor.

// Correct
print("Hello World")

This is a very common copy-and-paste issue for beginners.

Mistake 3: Misspelling the function name

Swift is case-sensitive. The function name must be written exactly as print.

Warning: Different capitalization creates a different, unknown name.

// Bad
Print("Hello World")

The correct spelling uses lowercase p.

// Correct
print("Hello World")

Learning case sensitivity early helps with all Swift code, not just print.

Mistake 4: Forgetting closing parentheses

Because function calls use parentheses, both the opening and closing parenthesis must be present.

// Bad
print("Hello World"

Add the missing closing parenthesis.

// Correct
print("Hello World")

This kind of syntax error becomes easier to spot as you get used to Swift's structure.

7. Best Practices

Practice 1: Use clear output messages

When you use print in learning code or debugging, make the message specific enough to be useful.

// Better than a vague message
print("User data loaded successfully")

A clear message helps you understand what happened when reading console output later.

Practice 2: Store repeated text in a constant

If the same message is used more than once, place it in a constant instead of repeating the literal.

let greeting = "Hello World"
print(greeting)
print(greeting)

This reduces duplication and makes later changes easier.

Practice 3: Use interpolation for dynamic output

When output depends on values in your program, use string interpolation instead of awkward manual combinations.

let name = "Ava"
print("Hello, \(name)!")

This is the standard Swift way to build readable output strings.

Practice 4: Use print for learning, not as your only debugging strategy

print is excellent for simple inspection, especially at the beginning, but larger projects often need breakpoints and proper debugging tools too.

let count = 3
print("Current count: \(count)")

This is helpful, but in more complex projects, combine it with Xcode's debugger rather than relying on printed output alone.

8. Limitations and Edge Cases

Swift also provides customizable output options such as separators and terminators, but those are extra features beyond the first-program stage.

9. Practical Mini Project

Now let’s build a tiny but complete Swift command-line program that starts with print("Hello World") and expands it into a short greeting sequence. This keeps the idea simple while showing how a real file can contain multiple statements and constants.

let programName = "My First Swift Program"
let userName = "Liam"

print("Hello World")
print("Welcome to \(programName)")
print("Hello, \(userName)!")
print("Program complete.")

This mini project shows several core ideas together:

If you save this in a Swift file and run it, you will see multiple lines of output. That is the natural next step after your first single-line program.

10. Key Points

11. Practice Exercise

Use what you learned to write a short Swift program that prints three lines:

Expected output: three lines of readable text in the console.

Hint: Create constants with let, then pass them to print. Use \(...) for interpolation.

let name = "Mia"
let favoriteNumber = 7

print("Hello World")
print("My name is \(name)")
print("My favorite number is \(favoriteNumber)")

This solution prints a fixed message, then outputs dynamic values using constants and string interpolation.

12. Final Summary

Your first Swift program, print("Hello World"), is small but important. It introduces function calls, string literals, parentheses, console output, and the idea that a Swift program is a sequence of valid statements. Once you understand that one line, you have already started learning the structure of Swift itself.

In this article, you saw how the syntax works, how to expand it with variables and interpolation, which common mistakes to avoid, and how to turn a one-line example into a small working program. A strong next step is to learn Swift variables and constants in more detail, then move on to user input, conditionals, and loops so your programs can do more than print fixed text.