Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 228
Caste Avatar asked Dec 15 '10 13:12

Caste


People also ask

Does Python have string interpolation?

Python 3.6 added new string interpolation method called literal string interpolation and introduced a new literal prefix f . This new way of formatting strings is powerful and easy to use. It provides access to embedded Python expressions inside string constants.

How is string interpolation performed in Python?

String interpolation is a process of injecting value into a placeholder (a placeholder is nothing but a variable to which you can assign data/value later) in a string literal. It helps in dynamically formatting the output in a fancier way. Python supports multiple ways to format string literals.

Which character should be used for string interpolation Python?

There are a number of different ways to format strings in Python, one of which is done using the % operator, which is known as the string formatting (or interpolation) operator.

What is string interpolation in Ruby?

String Interpolation, it is all about combining strings together, but not by using the + operator. String Interpolation works only when we use double quotes (“”) for the string formation. String Interpolation provides an easy way to process String literals.


2 Answers

Python 3.6 will add literal string interpolation similar to Ruby's string interpolation. Starting with that version of Python (which is scheduled to be released by the end of 2016), you will be able to include expressions in "f-strings", e.g.

name = "Spongebob Squarepants" print(f"Who lives in a Pineapple under the sea? {name}.") 

Prior to 3.6, the closest you can get to this is

name = "Spongebob Squarepants" print("Who lives in a Pineapple under the sea? %(name)s." % locals()) 

The % operator can be used for string interpolation in Python. The first operand is the string to be interpolated, the second can have different types including a "mapping", mapping field names to the values to be interpolated. Here I used the dictionary of local variables locals() to map the field name name to its value as a local variable.

The same code using the .format() method of recent Python versions would look like this:

name = "Spongebob Squarepants" print("Who lives in a Pineapple under the sea? {name!s}.".format(**locals())) 

There is also the string.Template class:

tmpl = string.Template("Who lives in a Pineapple under the sea? $name.") print(tmpl.substitute(name="Spongebob Squarepants")) 
like image 88
Sven Marnach Avatar answered Sep 29 '22 12:09

Sven Marnach


Since Python 2.6.X you might want to use:

"my {0} string: {1}".format("cool", "Hello there!") 
like image 26
EinLama Avatar answered Sep 29 '22 10:09

EinLama