2

I want to import a value inside a function which will work as an escape sequence on the string the function should print. Any help is greatly appreciated.

def vhf(c):
    print "...I want this \%s escape sequence" % c

vhf('n')

The output is :

...I want this \n escape sequence

But I want it to be:

...I want this
escape sequence
Ckrielle
  • 54
  • 9

2 Answers2

2

Since you aren't working with string literals, don't use escape sequences in the function.

def vhf(c):
    print "...I want this %s escape sequence" % (c,)

vhf('\n')
chepner
  • 497,756
  • 71
  • 530
  • 681
0

According to this thread, which discussed a similar issue, you could make use of the built-in String method decode together with the 'string-escape' codec:

def vhf(c):
    s = "...I want this \\" + c + " escape sequence"
    print s.decode('string_escape')
jofrev
  • 324
  • 3
  • 11