2018-10-12 04:30:38 +05:30
---
title: Python f-strings
---
# f-strings in Python
2018-11-08 21:36:17 +07:00
In Python version 3.6, a new method of formatting strings was implemented. The new method is called Literal string interpolation (though commonly referred to as an f-string, due to f prefix at the beginning of the string).
2018-10-12 04:30:38 +05:30
2018-11-08 21:36:17 +07:00
The use of f-string allows the programmer to dynamically insert a variable into a string in a clean and concise manner. In addition to inserting variables into a string this feature also provides the ability for a programmer to evaluate expressions, join the contents of collection, and even invoke functions within the f-string.
2018-10-12 04:30:38 +05:30
2018-10-20 13:18:36 -04:00
To perform these dynamic behaviours within an f-string we wrap them inside curly brackets within the string, and prepend a lower case f to the beginning of the string (before the opening quote).
2018-10-12 04:30:38 +05:30
2018-10-13 18:25:07 +11:00
## Examples
### Dynamically inserting a variable into a string at runtime:
#### Input
```python
name = 'Jon Snow'
2018-11-08 21:36:17 +07:00
greeting = f'Hello! {name}.'
2018-10-13 18:25:07 +11:00
print(greeting)
```
#### Output
```
2018-11-08 21:36:17 +07:00
Hello! Jon Snow.
```
### Multiple variables, of different types, can be inserted in the same string:
### Input
```python
item_id = 'P12305'
price = 425.50
print(f'Item {item_id} costs {price} USD.')
```
#### Output
```
Item P12305 costs 425.50 USD.
2018-10-13 18:25:07 +11:00
```
### Evaluate an expression in a string:
#### Input
```python
val1 = 2
val2 = 3
expr = f'The sum of {val1} + {val2} is {val1 + val2}'
print(expr)
```
#### Output
```
The sum of 2 + 3 is 5
```
### Calling a function and inserting output within a string:
#### Input
```python
def sum(*args):
result = 0
for arg in args:
result += arg
return result
func = f'The sum of 3 + 5 is {sum(3, 5)}'
print(func)
```
#### Output
```
The sum of 3 + 5 is 8
```
2019-06-10 20:52:01 -04:00
2018-10-13 18:25:07 +11:00
### Joining the contents of a collection within a string:
#### Input
```python
fruits = ['Apple', 'Banana', 'Pear']
list_str = f'List of fruits: {", ".join(fruits)}'
print(list_str)
```
#### Output
```
List of fruits: Apple, Banana, Pear
```
2019-06-10 20:52:01 -04:00
2018-10-19 16:40:32 -04:00
### Convert an integer to 8-bit binary
2019-06-10 20:52:01 -04:00
#### Input
2018-10-19 16:40:32 -04:00
```python
num = 42
print(f'The binary of {num} is {num:08b}')
```
2019-06-10 20:52:01 -04:00
#### Output
2018-10-19 16:40:32 -04:00
```
The binary of 42 is 00101010
```
2018-10-13 18:25:07 +11:00
2019-06-10 20:52:01 -04:00
### Using Object with f-strings
#### Input
```python
class Book:
def __init__ (self, title, author):
self.title = title
self.author = author
def __str__ (self):
return f"{self.title} by {self.author}"
book = Book("A Clash of Kings", "George R. R. Martin")
print(f"{book}")
```
##### Output
```
A Clash of Kings by George R. R. Martin
```
2018-10-12 04:30:38 +05:30
### Sources
https://www.python.org/dev/peps/pep-0498/