My Coding > Programming language > Python > Python FAQ > Python: f-string for printing variables

Python: f-string for printing variables

F-string in Python makes the formation of the test string from a few variables much simpler and I would like to encourage you to use f-string more often.

For example, if you want to print two variables, you can do the following code:


v1, v2 = 34, 45
print(f"My data: v1={v1}; v2={v2}")
# My data: v1=34; v2=45

But, if your goal is to print your variables with their names, you can use a special f-string feature:


print(f"my data: {v1=}; {v2=};")
# my data: v1=2; v2=44;

Isn't that simple? The equal sign inside the curly brackets will print the content of the expression in the curly brackets and then its value.


print(f"my data: {v1**2=}")
# my data: v1**2=4

It can be used for expressions as well.


Published: 2023-09-14 23:39:27

Last 10 artitles


9 popular artitles

© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit.