What is String Interpolation
String Interpolation is the process of evaluating a string literal and replacing placeholders with corresponding values.
In Python there are many ways of achieving string interpolation, for example str.format() and f-strings etc.
String Format
- In Python string format allows combing strings with integers. This is also known as interpolation.
- Python achieve this using a function named format.
- String format syntax is very simple, we just need to place a place holder {}where would like our integers and then in format function we need to specify the name of the variables.
String format Syntax
“Some text here {}”.format(someVariable)
In the above syntax the format() method will replace the place holder of curly brackets {} with the value of someVariable.
We can also pass many variables to format function, for example in below syntax we are using two place holders and format function will replace them with two variables.
“Some text here {} some more text here {}”.format(someVariable1, someVariable2)
Example of string format in Python
1 2 |
myAge = 23 print("Hi, I am John and I am {} years old".format(myAge)) |
In the above example we are showing a message and combining it with an integer 23 using format function. Format function will replace the empty curly brackets placeholder with the value of myAge variable.
Another example of string format
1 2 3 4 |
myAge = 23 myName = "John" myMessage = "myAge {}" print(myMessage.format(myAge)) |
Another example of string format
1 |
firstName = "Sara"<br />lastName = "Will"<br />age = 23<br />print("Hello {} {}, your age is {}".format(firstName, lastName, age)) |
In the older version of Python string format was achieved using a % sign. However, it’s deprecated now and we now use {} and format function in Python version 3 and upwards.
Here is an example of how interpolation was achieved in older python versions.
1 2 3 4 |
firstName = "Sara" lastName = "Will" age = 23 print("Hello %s %s, your age is %d" % (firstName, lastName, age)) |
%s was for Strings
%d was for Integers
%f – was Float
In Python 3 the % operator is deprecated and we now use {} placeholders and format function.
f string
f string is another way of achieving string interpolation. It’s much simpler.
String format Syntax
f”Some text here {variableHere}”
In the above syntax the the letter f is placed right before the string. Within the string we can write the name of our variables inside the place holder of curly brackets {}
We can place many variables in our string, for example in below syntax we are using two place holders with two variables.
f”Some text here {someVariable1} some more text here {someVariable2}”
Example of f string format in Python
1 2 |
myAge = 23 print(f"Hi, I am John and I am {myAge} years old") |
In the above example we are showing a message and combining it with an integer value 23 using f.
Another example of f string interpolation
1 |
firstName = "Sara"<br />lastName = "Will"<br />age = 23<br />print(f"Hello {firstName} {lastName}, your age is {age}") |