在Python中,可以在三引号中包含变量吗?如果是这样,怎么办?
发布于 2021-01-29 17:23:13
对于某些人来说,这可能是一个非常简单的问题,但这使我感到困惑。您可以在python的三引号内使用变量吗?
在下面的示例中,如何在文本中使用变量:
wash_clothes = 'tuesdays'
clean_dishes = 'never'
mystring =""" I like to wash clothes on %wash_clothes
I like to clean dishes %clean_dishes
"""
print(mystring)
我希望它导致:
I like to wash clothes on tuesdays
I like to clean dishes never
如果不是,在需要几个变量并且有大量文本和特殊字符的情况下,处理大块文本的最佳方法是什么?
关注者
0
被浏览
49
1 个回答
-
Python 2中的一种方法:
>>> mystring =""" I like to wash clothes on %s ... I like to clean dishes %s ... """ >>> wash_clothes = 'tuesdays' >>> clean_dishes = 'never' >>> >>> print mystring % (wash_clothes, clean_dishes) I like to wash clothes on tuesdays I like to clean dishes never
还要看一下字符串格式