Notice: Undefined offset: 0 in /var/sites/hackingwithswift.com/site/src/Controller/ExampleCodePageController.php on line 68
How to use string interpolation to combine strings, integers and doubles - free Swift 4 example code

NEW: Master Swift design patterns with my latest book! >>

How to use string interpolation to combine strings, integers and doubles

Written by Paul Hudson    @twostraws

String interpolation is Swift's way of letting you insert variables and constants into strings. But at the same time, you can also perform simple operations as part of your interpolation, such as changing letter case and basic mathematics. Swift is also smart enough to understand how to bring values into strings, meaning that you can use other strings, integers and floating-point numbers just fine.

Here's an example to get you started:

let name = "Paul"
let age = 35
let longestPi = 3.141592654

let combined = "This person's name is \(name.uppercased()), their age is \(age) so in \(age) years time they'll be \(age + age), and they know π up to \(longestPi)"

Available from iOS 7.0 – learn more in my book

Did this solution work for you? Please pass it on!

Other people are reading…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Help support Hacking with Swift

This site is funded by Hacking with Swift supporters who buy my e-books. If you can, please support my work – it comes packed with bonus material!

Click here to visit the Hacking with Swift store >>