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

前端 未结 9 1192
攒了一身酷
攒了一身酷 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:45

    Python 3.6 and newer have literal string interpolation using f-strings:

    name='world'
    print(f"Hello {name}!")
    

提交回复
热议问题