Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String interpolation with triple quotes and multiple lines

I'm getting stange results from triple """ quoted strings when using string interpolation and line breaks:

val foo = "bar"

s"""$foo"""

This is ok.

s"""
$foo
"""    

This is wrong, I get the following output:

"
bar
"

Why the heck are there quotation marks?

like image 331
0__ Avatar asked Apr 16 '13 13:04

0__


People also ask

Can triple quoted strings span multiple lines :?

Spanning strings over multiple lines can be done using python's triple quotes. It can also be used for long comments in code. Special characters like TABs, verbatim or NEWLINEs can also be used within the triple quotes.

What does 3 quotation marks mean?

The triple quotation is a nice way to be able to include other types of quotation within your string without having to use escape characters. For example: print("He said \"my name's John\"") That example requires escape characters \" to use double quote marks.

What do triple quotes surrounding a string signify?

Enclosing Strings Containing Single and Double Quotes Actually, we can use triple quotes (i.e., triplet of single quotes or triplet double quotes) to represent the strings containing both single and double quotes to eliminate the need of escaping any. >>> print('''She said, "Thank you! It's mine."''')

Can strings be enclosed in triple quotes?

String literals inside triple quotes, """ or ''', can span multiple lines of text.


1 Answers

This is just the REPL surrounding multi-line strings in quotation marks. You'll find the actual string contains no quotation marks:

res0.contains("\"")
res1 : Boolean = false
like image 159
Impredicative Avatar answered Oct 23 '22 03:10

Impredicative