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

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

    Python's string interpolation is similar to C's printf()

    If you try:

    name = "SpongeBob Squarepants"
    print "Who lives in a Pineapple under the sea? %s" % name
    

    The tag %s will be replaced with the name variable. You should take a look to the print function tags: http://docs.python.org/library/functions.html

提交回复
热议问题