Reputation: 51
What is meant, when they say "it matches the indentation of the closing quote?
“Use three double quotes (""") for strings that take up multiple lines. Indentation at the start of each quoted line is removed, as long as it matches the indentation of the closing quote. For example:
let quotation = """ Even though there's whitespace to the left, the actual lines aren't indented. Except for this line. Double quotes (") can appear without being escaped.
I still have (apples + oranges) pieces of fruit. """ Create arrays and dictionaries using brackets ([]), and access their elements by writing the index or key in brackets. A comma is allowed after the last element.”
Excerpt From: Apple Inc. “The Swift Programming Language (Swift 4).” iBooks. https://itunes.apple.com/us/book/the-swift-programming-language-swift-4/id881256329?mt=11
Upvotes: 4
Views: 4478
Reputation: 6067
These are the three scenarios I could think of to explain this:
Here the text and the triple quotes are aligned to the left
Check ouput, here the text is stored and print without a spaces at the begining of each paragraph.
let textSample1 = """
Test 1: Hello how are you welcome to the test
something else is written here
that's enough said
"""
print(textSample1)
Here the text has spacing at the begining but the triple quotes are aligned to the left
Check ouput, here the text is stored and print with spaces at the begining of each paragraph because the tripe quotes places at the left and they are taking into consideration those spaces in the paragraphs.
let textSample2 = """
Test 2: Hello how are you welcome to the test
something else is written here
that's enough said
"""
print(textSample2)
Here the text has spacing at the begining and the triple quotes also are spaced to match the text
Check ouput, here the text is stored and print without spaces at the begining though we have put spaces at the beginging, this is because the triple quotes are places at the same level as the text instead of the spaces so they spaces are ignored. I found this one handy when you want to store multi line text in code but wanted to wanted to maintain some code formatting among other uses to this.
let textSample3 = """
Test 3: Hello how are you welcome to the test
something else is written here
that's enough said
"""
print(textSample3)
OUTPUTS:
Test 1: Hello how are you welcome to the test
something else is written here
that's enough said
Test 2: Hello how are you welcome to the test
something else is written here
that's enough said
Test 3: Hello how are you welcome to the test
something else is written here
that's enough said
Upvotes: 14