Chapter 32: Swift Strings: Numbers and Strings

1. The Problem – Numbers are not Strings (and vice versa)

In Swift, numbers (Int, Double, Decimal, Float, etc.) and strings are completely different types.

You cannot directly concatenate them using +:

Swift

Error message you will see:

text

This is intentional — Swift is strict about types to prevent bugs.

So how do we solve this? There are several good ways — we’ll look at them from best → acceptable → avoid.

2. The Best & Most Recommended Way: String Interpolation \(…)

This is the #1 method almost every modern Swift developer uses.

Swift

Output:

text

Why interpolation is usually the best choice:

  • Very readable — the code looks almost like the output
  • Works with any type that can be converted to string
  • Handles spaces automatically (no need to add ” ” manually)
  • You can put expressions inside \(…) — not just variables
Swift

3. Formatting Numbers Inside Interpolation (very important)

By default, numbers can look ugly:

Swift

Better — use format specifiers or format styles:

Simple formatting with format:

Swift

Classic String(format:) style (still very common)

Swift

4. Other (less recommended) ways – you should know them

4.1 Using + with explicit conversion

Swift

Why avoid this style?

  • Verbose and easy to forget String(…)
  • You have to manually handle spaces
  • Much harder to read than interpolation

4.2 Using description or String(describing:)

Swift

When you see this: Usually in debugging or logging — not in user-facing strings.

5. Real-Life Examples – what you will actually write

Example 1 – User profile summary

Swift

Example 2 – Order confirmation message

Swift

Example 3 – Progress / status message

Swift

6. Quick Summary – How to Choose

Situation Best method Example
Building readable message with numbers Interpolation \(…) “Age: \(age), Score: \(score)”
Need specific number formatting Interpolation + format: \(money, format: .currency(code: “INR”))
Joining many strings (array / loop) .joined(separator:) parts.joined(separator: ” – “)
Building gradually in a loop .append(…) result.append(“Item \(i)\n”)
Old code / debugging + + String(…) “Count: ” + String(count)

7. Small Practice – Try these

  1. Create a profile summary with name, age, city, points using interpolation
  2. Show an order total with 18% GST, formatted nicely with ₹ symbol and commas
  3. Build a simple progress message: completed = 6, total = 10 → “Progress: 6/10 (60.0%)”

Paste your code if you want feedback!

What would you like to explore next about strings?

  • String formatting in much more depth (numbers, dates, currencies…)
  • Splitting, joining, replacing, trimming
  • Regular expressions with strings
  • Working with substrings & string indices
  • Strings in SwiftUI (Text, labels, markdown)
  • Or move to another topic (arrays, optionals, control flow…)

Just tell me — we’ll continue in the same clear, detailed, patient style 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *