python - 如何格式化函数内的转义序列

标签 python python-2.7 function formatting escaping

我想在函数内导入一个值,该值将用作函数应打印的字符串的转义序列。非常感谢任何帮助。

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

vhf('n')

输出是:

...I want this \n escape sequence

但我希望它是:

...I want this
escape sequence

最佳答案

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

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

vhf('\n')

关于python - 如何格式化函数内的转义序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56936785/

相关文章:

python - 在 python 中使用 zerorpc 对项目进行 dockerizing 时导入错误

postgresql - 获取函数复制的数据

python - 在每个可能的组合中调用函数

python - debian 8.2 中的 "pip is configured with locations that require TLS/SSL"

python - for循环被用作while循环(python)

python-3.x - Tkinter 在 Python 3 上找到但在 Python2.7 上找不到

function - haskell 中的函数 (.).(.)

r - 将变量名称分配给具有多个变量的数据框中的非 NA

python - 在 Pandas 中,如何仅使用 True 系列 bool 值对行进行求和

python 从 url 列表中抓取网页