1

I read that anything between triple quotes inside print is treated literal so tried messing things a little bit. Now I am not able to get above statement working. I searched internet but could not find anything.

statement:

print("""Hello World's"s""""")

Output I am getting:

Hello World's"s

Expected output:

Hello World's"s""
Druta Ruslan
  • 7,171
  • 2
  • 28
  • 38
dvsakgec
  • 3,514
  • 4
  • 28
  • 35
  • A string which starts with `"""` ends at the first occurrence of `"""` after that, so the whole string with quotes is `"""Hello World's"s"""` and it is followed by an empty string withing single quotes `""`. It is valid to put two string one after the other and it is same as concatenating it. So, your input is equivalent to `"""Hello World's"s""" ""` – zvone May 26 '18 at 18:18

5 Answers5

3

print("""Hello World's"s""""") is seen as print("""Hello World's"s""" "") because when python find """ it automatically ends the previous string beginning with a triple double-quote.

Try this:

>>> print("a"'b')
ab

So basically your '"""Hello World's"s"""""' is just <str1>Hello World's"s</str1><str2></str2> with str2 an empty string.

politinsa
  • 3,480
  • 1
  • 11
  • 36
2

Triple quoted string is usually used for doc-string. As @zimdero pointed out Triple-double quote v.s. Double quote
You can also read https://stackoverflow.com/a/19479874/1768843
And https://www.python.org/dev/peps/pep-0257/

If you really want to get the result you want just use \" or just you can do combination with ``, .format() etc

print("Hello World's\"s\"\"")

https://repl.it/repls/ThatQuarrelsomeSupercollider

pagep
  • 3,488
  • 1
  • 26
  • 47
  • The `""` and `""" """` string should be same. Sure there are some benefits for using `""" """` but it's not necessary. Anyway thanks for reminding that. – pagep May 26 '18 at 16:53
  • 1
    Triple quotes are useful anywhere you want a newline to be part of the text. They are also useful if you want`"` and `'` to be part of the text. But as OP found out, you still need to escape triple quotes inside triple quotes. – tdelaney May 26 '18 at 16:55
1

Triple quotes within a triple-quoted string must still be escaped for the same reason a single quote within a single quoted string must be escaped: The string parsing ends as soon as python sees it. As mentioned, once tokenized your string is equivalent to

"""Hello World's"s""" ""

That is, two strings which are then concatenated by the compiler. Triple quoted strings can include newlines. Your example is similar to

duke = """Thou seest we are not all alone unhappy:
This wide and universal theatre
Presents more woeful pageants than the scene
Wherein we play in."""

jaques = """All the world's a stage,
And all the men and women merely players:
They have their exits and their entrances;
And one man in his time plays many parts."""

If python was looking for the outermost triple quotes it would only have defined one string here.

tdelaney
  • 73,364
  • 6
  • 83
  • 116
0

Simple with ''' to not complicate things:

print('''Hello World's"s""''')
Anton vBR
  • 18,287
  • 5
  • 40
  • 46
-1

Maybe this is what you are looking for?

print("\"\"Hello World's's\"\"")

Output:

""Hello World's's""
loksoni
  • 177
  • 3
  • 12