Let's say I have this string literal with line breaks:
file :: String file = "the first line\nthe second line\nthe third line"
Is there any way to write it like this?
file :: String file = "the first line the second line the third line"
The attempt just above leads to this error:
factor.hs:58:33: lexical error in string/character literal at character '\n' Failed, modules loaded: none.
Use triple quotes to create a multiline string It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.
Raw StringsThey can span multiple lines without concatenation and they don't use escaped sequences. You can use backslashes or double quotes directly.
Three single quotes, three double quotes, brackets, and backslash can be used to create multiline strings.
A multiline string in Python begins and ends with either three single quotes or three double quotes. Any quotes, tabs, or newlines in between the “triple quotes” are considered part of the string.
You can write multiline strings like so
x = "This is some text which we escape \ \ and unescape to keep writing"
Which prints as
"This is some text which we escape and unescape to keep writing"
If you want this to print as two lines
x = "This is some text which we escape \n\ \ and unescape to keep writing"
Which prints as
This is some text which we escape and unescape to keep writing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With