2

How would you go about converting a string into a triple quoted string?

x = "y"

into:

x = """y"""
  • This question is related to http://stackoverflow.com/questions/10840357/string-literal-with-triple-quotes-in-function-definitions – Mihai8 Feb 03 '13 at 12:33
  • 1
    This isn't a programming question, it's an editor question I guess, but you never mention your editor. What is the point of this? Do a search replace with regex in your editor of choice. Voting to close – Josh Smeaton Feb 03 '13 at 12:39
  • @JoshSmeaton: It isn't even clear if that is what the OP is trying to do. Is the OP perhaps processing python source code files? Is the OP trying to turn single quotes to tripple quotes in a specific editor? Or does the OP expect that tripple quotes result in different Python values than single quotes? I guessed at that 3rd interpretation, but as it stands it could be any of those 3 options or perhaps a 4th or 5th one that I didn't think of yet. – Martijn Pieters Feb 03 '13 at 12:41
  • 2
    What are you trying to do? – johnharris85 Feb 03 '13 at 12:43
  • @user, I believe you're confused: Once you create a string with `x = "y"`, the string just contains `y` -- the same it would contain if you'd created it with `x = """y"""`. The quotes are _not_ in the string. So there's nothing to convert. – alexis Feb 03 '13 at 18:27

2 Answers2

4

You don't. A tripple-quoted string is just notation sugar, it results in a normal Python string regardless:

>>> x = "y"
'y'
>>> x = """y"""
'y'

You use tripple quotes to make inclusion of newlines easier:

"""\
Some longer string
with newlines in the text
is easier this way.
"""

is easier to read than:

"Some longer string\nwith newlines in the text\nis easier this way.\n"

but the end result for these two ways of declaring a string value is exactly the same.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Theoretically speaking would it be possible? –  Feb 03 '13 at 12:33
  • @user1656854: What are you trying to achieve? Change python source code? Theoretically, you can create the same values with either single or tripple-quoted strings. – Martijn Pieters Feb 03 '13 at 12:37
0

You can escape quotes using \ character. I don't know exactly how does Python contacnate strings, but it would look similar to:

x = "\"\"y\"\"";
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778