In Python, can you have variables within triple quotes? If so, how?

后端 未结 7 1520
栀梦
栀梦 2020-12-14 05:33

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

7条回答
  •  时光说笑
    2020-12-14 05:52

    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

    • http://docs.python.org/library/string.html#string-formatting

提交回复
热议问题