Ruby example:
name = "Spongebob Squarepants"
puts "Who lives in a Pineapple under the sea? \n#{name}."
The successful Python string concatenation is seemingly verbose to me.
Ruby example:
name = "Spongebob Squarepants"
puts "Who lives in a Pineapple under the sea? \n#{name}."
The successful Python string concatenation is seemingly verbose to me.
Python's string interpolation is similar to C's printf()
If you try:
The tag
%s
will be replaced with thename
variable. You should take a look to the print function tags: http://docs.python.org/library/functions.htmlYou can also have this
http://docs.python.org/2/library/string.html#formatstrings
Usage:
PS: performance may be a problem. This is useful for local scripts, not for production logs.
Duplicated:
Python string formatting: % vs. .format
What is the Python equivalent of embedding an expression in a string? (ie. "#{expr}" in Ruby)
What is Ruby equivalent of Python's `s= "hello, %s. Where is %s?" % ("John","Mary")`
Is there a Python equivalent to Ruby's string interpolation?
Since Python 2.6.X you might want to use:
I've developed the interpy package, that enables string interpolation in Python.
Just install it via
pip install interpy
. And then, add the line# coding: interpy
at the beginning of your files!Example:
For old Python (tested on 2.4) the top solution points the way. You can do this:
And you get