Is there a Python equivalent to Ruby's string interpolation?

前端 未结 9 1166
攒了一身酷
攒了一身酷 2020-11-22 17:04

Ruby example:

name = \"Spongebob Squarepants\"
puts \"Who lives in a Pineapple under the sea? \\n#{name}.\"

The successful Python string co

9条回答
  •  轮回少年
    2020-11-22 17:52

    String interpolation is going to be included with Python 3.6 as specified in PEP 498. You will be able to do this:

    name = 'Spongebob Squarepants'
    print(f'Who lives in a Pineapple under the sea? \n{name}')
    

    Note that I hate Spongebob, so writing this was slightly painful. :)

提交回复
热议问题