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

后端 未结 7 1522
栀梦
栀梦 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 06:13

    I think the simplest way is str.format() as others have said.

    However, I thought I'd mention that Python has a string.Template class starting in Python2.4.

    Here's an example from the docs.

    >>> from string import Template
    >>> s = Template('$who likes $what')
    >>> s.substitute(who='tim', what='kung pao')
    'tim likes kung pao'
    

    One of the reasons I like this is the use of a mapping instead of positional arguments.

    0 讨论(0)
提交回复
热议问题