三引号内可以有变量吗?如果是这样,怎么做? [英] Can you have variables within triple quotes? If so, how?

查看:16
本文介绍了三引号内可以有变量吗?如果是这样,怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

This is probably a very simple question for some, but it has me stumped. Can you use variables within python's triple-quotes?

In the following example, how do use variables in the text:

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 would like it to result in:

 I like to wash clothes on tuesdays
     I like to clean dishes never

If not what is the best way to handle large chunks of text where you need a couple variables, and there is a ton of text and special characters?

解决方案

One of the ways in 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

Also look at string formatting

这篇关于三引号内可以有变量吗?如果是这样,怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆