Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Basics (retired) Types Printing Results

"" + world vs \(world)

What's the difference between using (if world is a variable)

print("Hello" + world) and print ("Hello (world)")?

Hi Kaitlan, both are correct. You can check more on that here - Printing.

Good point, thanks for link, thought I'd jumped the gun after id posted.. :)

No worries, I have done that sometimes too haha :D

3 Answers

Jeremy Hayden
Jeremy Hayden
1,740 Points

To see the difference try this code in your playground:

var world = "world"
print ("hello \(world)")
print ("hello " + world)

Now change the value of world to 1.

var world = 1
print ("hello \(world)")
print ("hello " + world)

You will get errors on "hello " + world, but not on "hello (world)"

You can't + or concatenate a string with a integer. Now if you are sure that world will only be a string, then your + world will work. But if there is any possibility that you want world to be a different data type, it is safer to use string interpolation, that is (world)

I guess you mean...

print("Hello" + world) 
print ("Hello \(world)")

From what I understand the first shows concatenation which is joining two string values together or string - character and the 2nd one shows interpolation. With interpolation you can mix up stings with variables that are not strings or characters, for example numbers but when it comes to concatenation you can't. You can read more on that, on Apple's developers documentation - Strings.

Hi Gloria, comment not related to course but excited to see another female doing swift. There are not many

Thanks Kaitlan, I haven't really started iOS because I don't have a MAC yet, but I like C and swift reminds me of it so It makes it easier to understand. Thank you :D Yes it is nice seeing female developers.

Hi I assume you meant to insert the slash as typed below

println("Hello" + world) and print ("Hello (world)")

Answer, in this instance there is not a great deal of difference however in general use the first one just 'concantenates' to 'Hello' the benifet of 2nd one is you only have to have one set of quote marks to keep track of and can insert variables in context of sentence to your hearts content while interspaced with general text from println content anywhere you want. If you wish to achieve the same using first example with a complex sentence with text and variables it gets very messy.