String substitution in Python 3?

后端 未结 4 467
陌清茗
陌清茗 2021-02-05 21:20

I\'m trying to do something like this:

subs = \'world\'
\"Hello {[subs]}\"

in Python 3.

I can\'t quite figure out the syntax (having co

4条回答
  •  野的像风
    2021-02-05 21:55

    Like others pointed out already, there are two ways to do string formatting in Python at the moment.

    Python 3.6 will introduce a third option. Details can be found here:

    PEP 0498 -- Literal String Interpolation

    >>> name = 'Fred' 
    >>> f'He said his name is {name!r}.'  
    "He said his name is 'Fred'."
    

提交回复
热议问题