Menu
CoddyTech

Strings and Interpolation

Lesson 6 of 48 in Coddy's Swift for Go Developers course.

Go fmt formatting inserts values through format verbs, while Swift String interpolation uses a backslash and parentheses. The literal percent sign is escaped as %% in the source format string. Text construction and printing can be separated with a returned formatted string.

Go strings are immutable byte sequences, while Swift String has value semantics with supported mutation through var. Replacing a copied Go string binding does not mutate its earlier bytes. Swift let prevents mutation of the String value, but its character abstraction differs from Go byte indexing.

Go: complete program

main.go

package main
import (
    "fmt"
)

func main() {
percent := 40
    fmt.Printf("Downloaded %d%%\n", percent)
}

Swift: complete program

main.swift

import Foundation
let percent = 40
print("Downloaded \(percent)%")

This ASCII output example only formats a percentage. A Go string offset does not automatically correspond to a Swift Character position.

challenge icon

Challenge

Easy

Implement badge(_:team:). Input is two ASCII words of 1 to 20 letters: first name then team. Output the team in square brackets, one space, then the name.

Implement the required APIs and solve(_ input: String) -> String. Return output text without a final newline; do not print in editable code. The locked driver independently checks the APIs on additional valid inputs, then prints the returned text with exactly one newline. All numeric operations stay within Int bounds.

Your code goes in Functions.swift. main.swift holds the supplied input/output code and cannot be edited.

Try it yourself

import Foundation
func apiChecks(_ input: String) -> Bool {
return badge("Ivo", team: "Oak") == "[Oak] Ivo" && badge("Lee", team: "Pine") == "[Pine] Lee"
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// BEGIN PUBLIC API PROBES
let apiOK = apiChecks(input)
// END PUBLIC API PROBES
if apiOK { print(solve(input)) } else { print("API mismatch") }
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Swift for Go Developers

Practice on your own: Swift playground