如何格式化函数中的转义序列

时间:2019-07-08 14:10:20

标签: python python-2.7 function formatting escaping

我想在函数内部导入一个值,该值将作为函数应打印的字符串上的转义序列。任何帮助将不胜感激。

Linking.openURL('fb://page/<ID-PAGE>');

输出为:

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

vhf('n')

但是我希望它是:

...I want this \n escape sequence

2 个答案:

答案 0 :(得分:2)

由于您不使用字符串文字,因此请勿在函数中使用转义序列。

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

vhf('\n')

答案 1 :(得分:0)

根据this线程,该线程讨论了类似的问题,您可以将内置的String方法decode'string-escape' { {3}}:

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