single-quoted string conversion to triple-quoted string

Jeff Shannon jeff at ccvcorp.com
Fri Apr 5 20:54:51 EST 2002


In article 
<GZqr8.20207$ml2.1517221 at newsread1.prod.itd.earthlink.net>, 
robinjim at earthlink.net says...
> Is there a way to convert a single quoted string to a triple quoted string?
> 
> For example, given:
> 
> ''' abc %d xyz '''
> 
> the result of ''' abc %d xyz''' % 10 is:
> 
> ' abc 10 xyz '
> 
> which is a single quoted string.
> 
> I would like to convert the result to a triple-quoted string.

In Python, there is no internal difference between single-quoted 
and triple-quoted strings -- they're all just strings.  The 
triple-quote trick is just a way to allow string literals to more 
easily contain characters that would be difficult to include 
otherwise.  Notable among those characters are newlines, single 
quotes, and double quotes.  However, once they're parsed, there's 
no difference at all.

>>> triple = """This is a string with 
... a newline and a " quote."""
>>> double = "This is a string with \na newline and a \x22 
quote."
>>> single = 'This is a string with \na newline and a " quote.'
>>> triple == double
1
>>> triple == single
1
>>> double == single
1
>>> 

As you see, the only difference between quoting styles, is a 
matter of what's easiest to type/read.

-- 

Jeff Shannon
Technician/Programmer
Credit International



More information about the Python-list mailing list