1

Just wondering...

I find using escape characters too distracting. I'd rather do something like this (console code):

>>> print ^'Let's begin and end with sets of unlikely 2 chars and bingo!'^
Let's begin and end with sets of unlikely 2 chars and bingo!

Note the ' inside the string, and how this syntax would have no issue with it, or whatever else inside for basically all cases. Too bad markdown can't properly colorize it (yet), so I decided to <pre> it.

Sure, the ^ could be any other char, I'm not sure what would look/work better. That sounds good enough to me, tho.

Probably some other language already have a similar solution. And, just maybe, Python already have such a feature and I overlooked it. I hope this is the case.

But if it isn't, would it be too hard to, somehow, change Python's interpreter and be able to select an arbitrary (or even standardized) syntax for notating the strings?

I realize there are many ways to change statements and the whole syntax in general by using pre-compilators, but this is far more specific. And going any of those routes is what I call "too hard". I'm not really needing to do this so, again, I'm just wondering.

Community
  • 1
  • 1
cregox
  • 17,674
  • 15
  • 85
  • 116
  • 3
    I'd've thought that having your choice of 'string' '''string''' "string" and """string""" would've been enough for almost all situations. What am I missing? – Blair Conrad May 11 '10 at 19:04
  • 1
    @Blair Conrad, you're also missing the permutations of those strings with the prefix R, e.g. R"string". – Mark Ransom May 11 '10 at 19:09
  • @Blair actually you missed "maybe I overlooked it". ;-) – cregox May 11 '10 at 21:46
  • @Mark Ransom, true enough. and the ones with the prefix r. But I figured the rstrings were outside of the scope of the original question - it seemed to be more focused on the delimiters rather than embedded escapes. – Blair Conrad May 12 '10 at 16:52

1 Answers1

13

Python has this use """ or ''' as the delimiters

print '''Let's begin and end with sets of unlikely 2 chars and bingo'''

How often do you have both of 3' and 3" in a string

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
  • Not often, but it happens. Thanks a lot! :) - That's also much better looking than my weak suggestion. Freaking great it's there already! – cregox May 11 '10 at 21:45
  • 1
    @Cawas, that's not an uncommon reaction to Python. "Batteries Included" isn't the half of it. – Mark Ransom May 12 '10 at 17:34
  • On a related note, what's that syntax I see in some codes using `"""` to quote "comments" right below any `def`? – cregox May 13 '10 at 15:35
  • They are docstrings http://www.python.org/dev/peps/pep-0257/ The standard way of documenting functions etc – mmmmmm May 14 '10 at 08:23