[Tutor] Raw string

Lie Ryan lie.1296 at gmail.com
Mon Jul 21 20:13:32 CEST 2008

> Thanks,
> I am aware of goodies that raw string offers, but my question was 
> how to use it with variable that already contains string.  :)

If you really have to, you may use something like this:

# Untested
def kludge(s):
    s = 'r"""%s"""' % repr(s)
    return eval(s)

Most people would frown at the use of eval (in fact I do too), because
it is possible to leak dangerous code from this function, given certain
string sequences.

But, that's enough of a kludge and don't ask for more kludges. THE
correct way to fix the problem is not here, but in the input function
(the function that assigns the variable)...

If you're reading from a file, and you get this problem, most probably
the program that generates the file is the problem. Check the file's
real content...

If you're reading from standard input, make sure you use raw_input(),
instead of input(), as text in input() is eval()ed first, not something
we would want... 

More information about the Tutor mailing list