什么意思,当他们说,“它符合缩进的结尾引号?”
“对占用多行的字符串使用三个双引号(”)。只要与结束引号的缩进匹配,则删除每个引号开头的缩进。例如:
让引号=“”即使左边有空格,实际的行也不会缩进。除了这一行。双引号(")可以不被转义。
我还有(苹果+橘子)水果。“”使用方括号([])创建数组和字典,并通过在括号中写入索引或键来访问它们的元素。最后一个元素之后允许使用逗号。
摘录自:苹果公司。“Swift编程语言(Swift 4)”iBooks。https://itunes.apple.com/us/book/the-swift-programming-language-swift-4/id881256329?mt=11
发布于 2017-11-11 09:29:25
我可以想到以下三种情况来解释这一点:
这里,文本和三元引号对齐左
检查输出,这里的文本存储和打印没有空格在开始每一段。
let textSample1 = """
Test 1: Hello how are you welcome to the test
something else is written here
that's enough said
"""
print(textSample1)
这里的文本开头有空格,但三元引号与左对齐。
检查输出,这里的文本存储和打印在每个段落开头的空格,因为tripe引号在左边,他们正在考虑在段落中的那些空格。
let textSample2 = """
Test 2: Hello how are you welcome to the test
something else is written here
that's enough said
"""
print(textSample2)
这里,文本在开头有空格,三引号也被空格以匹配文本。
检查输出,这里存储文本并在开始时打印没有空格--虽然我们把空格放在开头,这是因为三元引号与文本的位置相同,而不是空格,因此它们的空格被忽略了。当您想要在代码中存储多行文本时,我发现这是一种很方便的方法,但是我想要在这方面维护一些代码格式设置和其他用途。
let textSample3 = """
Test 3: Hello how are you welcome to the test
something else is written here
that's enough said
"""
print(textSample3)
输出:
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
https://stackoverflow.com/questions/47234762
复制相似问题