๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ formatted-string-literals-f-strings-python
f-strings in Python - GeeksforGeeks
May 16, 2026 - They allow variables and expressions to be directly embedded inside strings using curly braces {}. ... An f-string is created by adding f before the string and placing variables or expressions inside {}. ... Example 1: This example formats and ...
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ inputoutput.html
7. Input and output โ€” Python 3.15.0 documentation
Formatted string literals (also called f-strings for short) let you include the value of Python expressions inside a string by prefixing the string with f or F and writing expressions as {expression}. An optional format specifier can follow ...
๐ŸŒ
Packetcoders
packetcoders.io โ€บ how-to-print-a-variable-and-its-value-using-python-f-strings
How to Print a Variable and Its Value Using Python f-Strings
July 20, 2026 - Python Tip: Append the variable with = to also show the variable name when using Python f-string's. Heres an example ๐Ÿ‘‡ x, y, z = 1, 2, 3 f"{x} {y} {z}" # Output: # '1 2 3' f"{x=} {y=} {z=}" # Output: # 'x=1 y=2 z=3'
๐ŸŒ
Real Python
realpython.com โ€บ python-f-strings
Python's F-String for String Interpolation and Formatting โ€“ Real Python
November 30, 2024 - >>> variable = "Some mysterious value" >>> print(f"{variable = }") variable = 'Some mysterious value' You can use a variable name followed by an equal sign (=) in an f-string to create a self-documented expression. When Python runs the f-string, it builds an expression-like string containing the variableโ€™s name, the equal sign, and the variableโ€™s current value.
๐ŸŒ
Littlecolumns
littlecolumns.com โ€บ learn โ€บ python โ€บ f-strings
Formatting output with f strings - Python's Not (Just) For Unicorns
And if you think thatโ€™s amazing, maybe you should just skip the next section, because your life is about to change forever. When youโ€™re using your variable, you can also add weird little modifiers to tell Python how you want the variable displayed. Itโ€™s mostly used with numbres - you can add commas to big numbers, round long decimal numbers, left- or right-align, or a hundred other things! years = 65000000 print(f'Tyrannosaurus rex roamed the earth {years:,} years ago') pi_value = 3.14159265 print(f'The value of pi is roughly {pi_value:.3f}') id_number = 3 print(f'Your ID number is {id_number:04}')
๐ŸŒ
Boot.dev
boot.dev โ€บ lessons โ€บ 9c216a9b-6467-4526-88f5-02678df98d77
Learn Python for Beginners: F-Strings in Python | Boot.dev
In Python, we can create strings ... of quotes to create an f-string: f"this is easy!" Use curly brackets {} around a variable to interpolate (put) its value into the string....
๐ŸŒ
Bentley
cissandbox.bentley.edu โ€บ sandbox โ€บ wp-content โ€บ uploads โ€บ 2022-02-10-Documentation-on-f-strings-Updated.pdf pdf
A Guide to Formatting with f-strings in Python - CIS Sandbox
Updated in version 3.9 of Python is the use of f-strings for debugging purposes. The following ยท shows how you can use f-strings to display the value of a variable in the form: variable
๐ŸŒ
ZetCode
zetcode.com โ€บ python โ€บ fstring
Python f-string - formatting strings in Python with f-string
May 11, 2025 - F-strings allow you to dynamically set the width and precision of your formatted output by using variables instead of hardcoded values. This provides great flexibility when you need to adjust formatting based on runtime conditions or user preferences. ... #!/usr/bin/python value = 123.456789 ...
๐ŸŒ
Built In
builtin.com โ€บ data-science โ€บ python-f-string
Guide to String Formatting in Python Using F-strings | Built In
To follow this tutorial, make sure you have Python 3.6 or above. Otherwise, f-strings wonโ€™t work. F-string provides a better syntax for adding variables to strings. Letโ€™s print two messages using f-string in Python. To do that, we have to add the f in front of the โ€˜โ€™ that we use to create regular strings.
Find elsewhere
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-f-strings-tutorial-how-to-use-f-strings-for-string-formatting
Python f-String Tutorial โ€“ String Formatting in Python Explained with Code Examples
September 14, 2021 - As f-Strings are evaluated at runtime, you might as well evaluate valid Python expressions on the fly. โ–ถ In the example below, num1 and num2 are two variables. To calculate their product, you may insert the expression num1 * num2 inside a set of curly braces. num1 = 83 num2 = 9 print(f"The product of {num1} and {num2} is {num1 * num2}.")
๐ŸŒ
The Python Code
thepythoncode.com โ€บ article โ€บ print-variable-name-and-value-in-python
How to Print Variable Name and Value in Python - The Python Code
However, there is a faster and even more convenient way to print the variable name and value using the "=" sign in f-strings: ... Remember that this syntax (using the "=" sign) is only available in Python 3.8 and above.
๐ŸŒ
Real Python
realpython.com โ€บ lessons โ€บ simpler-debugging-f-strings
Simpler Debugging With F-Strings (Video) โ€“ Real Python
02:57 And you could use an f-string and inside of it, say python and then just add an equal sign (=). With this new debugging specifier, when you add that equal sign to the end of the expression, itโ€™s going to print both the expression and ...
Published: November 5, 2019
๐ŸŒ
Medium
medium.com โ€บ gustavorsantos โ€บ f-string-the-easiest-way-to-print-in-python-82f9aca01855
F-string: the easiest way to print() in Python - gustavorsantos - Medium
December 8, 2022 - The easiest way to print a message in Python is using f-strings. Present since version 3.6, the โ€œf-stringโ€ came to make prints that mix text and variables as easy as possible.
๐ŸŒ
Medium
medium.com โ€บ bitgrit-data-science-publication โ€บ python-f-strings-tricks-you-should-know-7ce094a25d43
Python F-strings Tricks You Should Know | by Benedict Neo | bitgrit Data Science Publication | Medium
October 12, 2022 - Python Python F-strings Tricks You Should Know Stop doing print(fโ€variable = {variable}โ€) Python 3.6, all the way back in 2016, introduced a new string formatting method called f-strings, which โ€ฆ
๐ŸŒ
Medium
medium.com โ€บ @venu_madhav โ€บ master-the-python-f-string-26a426459bdb
Python variables | python print funtion | python loops | python control statements | python class | ML with python | lists | python f-strings | Medium
March 25, 2024 - Just prefix your string with โ€˜fโ€™ or โ€˜Fโ€™, and then place your variables or expressions inside curly braces {}. Python will automatically replace them with their values. ... name = "Pythonista" age = 25 print(f"Hello, {name}! You are {age} years old.") # Hello, Pythonista!
๐ŸŒ
Python Guides
pythonguides.com โ€บ print-a-string-and-variable-in-python
Python Print Variable: Print a String and Variable Together
October 14, 2025 - Add = inside an f-string: print(f"{price=}") prints price=19.99. It needs Python 3.8 or later.
Top answer
1 of 6
103

f"..." strings are great when interpolating expression results into a literal, but you don't have a literal, you have a template string in a separate variable.

You can use str.format() to apply values to that template:

name=["deep","mahesh","nirbhay"]
user_input = "certi_{element}" # this string i ask from user  

for value in name:
    print(user_input.format(element=value))

String formatting placeholders that use names (such as {element}) are not variables. You assign a value for each name in the keyword arguments of the str.format() call instead. In the above example, element=value passes in the value of the value variable to fill in the placeholder with the element.

Unlike f-strings, the {...} placeholders are not expressions and you can't use arbitrary Python expressions in the template. This is a good thing, you wouldn't want end-users to be able to execute arbitrary Python code in your program. See the Format String Syntax documenation for details.

You can pass in any number of names; the string template doesn't have to use any of them. If you combine str.format() with the **mapping call convention, you can use any dictionary as the source of values:

template_values = {
    'name': 'Ford Prefect',
    'number': 42,
    'company': 'Sirius Cybernetics Corporation',
    'element': 'Improbability Drive',
}

print(user_input.format(**template_values)

The above would let a user use any of the names in template_values in their template, any number of times they like.

While you can use locals() and globals() to produce dictionaries mapping variable names to values, I'd not recommend that approach. Use a dedicated namespace like the above to limit what names are available, and document those names for your end-users.

2 of 6
54

If you define:

def fstr(template):
    return eval(f"f'{template}'")

Then you can do:

name=["deep","mahesh","nirbhay"]
user_input = r"certi_{element}" # this string i ask from user  

for element in name:
    print(fstr(user_input))

Which gives as output:

certi_deep
certi_mahesh
certi_nirbhay

But be aware that users can use expressions in the template, like e.g.:

import os  # assume you have used os somewhere
user_input = r"certi_{os.environ}"

for element in name:
    print(fstr(user_input))

You definitely don't want this!

Therefore, a much safer option is to define:

def fstr(template, **kwargs):
    return eval(f"f'{template}'", kwargs)

Arbitrary code is no longer possible, but users can still use string expressions like:

user_input = r"certi_{element.upper()*2}"

for element in name:
    print(fstr(user_input, element=element))

Gives as output:

certi_DEEPDEEP
certi_MAHESHMAHESH
certi_NIRBHAYNIRBHAY

Which may be desired in some cases.

๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-print-variable-how-to-print-a-string-and-variable
Python Print Variable โ€“ How to Print a String and Variable
December 7, 2021 - To print anything in Python, you use the print() function โ€“ that is the print keyword followed by a set of opening and closing parentheses,(). #how to print a string print("Hello world") #how to print an integer print(7) #how to print a variable ...
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-f-string
Python f-string: A Complete Guide | DataCamp
December 3, 2024 - Since Python 3.8, f-strings have become even more useful for debugging with the addition of the '=' specifier. This handy feature displays both variable names and their values in a single line: x = 10 y = 20 print(f"{x=}, {y=}") # Output: x=10, y=20
Top answer
1 of 15
358

Python 3.8 f-string = syntax

It has arrived!

#!/usr/bin/env python3
foo = 1
bar = 2
print(f"{foo=} {bar=}")

output:

foo=1 bar=2 

You can also ask for spaces before/after the = sign on the output by simply adding spaces around = in {}thanks, e.g.

foo = 1
bar = 2
print(f"{foo = } {bar =}")

produces:

foo = 1 bar =2

This feature was added in commit https://github.com/python/cpython/commit/9a4135e939bc223f592045a38e0f927ba170da32 "Add f-string debugging using '='." which documents:

f-strings now support =  for quick and easy debugging
-----------------------------------------------------

Add ``=`` specifier to f-strings. ``f'{expr=}'`` expands
to the text of the expression, an equal sign, then the repr of the
evaluated expression.  So::

  x = 3
  print(f'{x*9 + 15=}')

Would print ``x*9 + 15=42``.

so it also works for arbitrary expressions. Nice!

Visual Studio Code snippet

I use a variant of this for every language/IDE I ever use to quickly add debug print statements, for Python VS Code it is:

"Print variable to console": {
    "prefix": "pp",
    "body": [
        "print('${CLIPBOARD} = ' + str(${CLIPBOARD}))"
    ],
    "description": "Log variable to console"
}

Then you can just select a variable name, Ctrl + C, put cursor somewhere, pp + Enter, and then you get:

print('variablename = ' + str(variablename))

For temporary debug statements, this or a variant of it is a huge time saver.

The dream: JavaScript-like dict keys from variable names

I find Python better than JavaScript in almost every sense, but I've grown to really like this JavaScript feature:

let abc = 1
let def = 2
console.log({abc, def})

works in JavaScript because {abc, def} expands to {abc: 1, def: 2}. This is just awesome, and gets used a lot in other places of the code besides logging.

Not possible nicely in Python currently except with locals: Python variables as keys to dict

2 of 15
35

You can just use eval:

def debug(variable):
    print variable, '=', repr(eval(variable))

Or more generally (which actually works in the context of the calling function and doesn't break on debug('variable'), but only on CPython):

from __future__ import print_function

import sys

def debug(expression):
    frame = sys._getframe(1)

    print(expression, '=', repr(eval(expression, frame.f_globals, frame.f_locals)))

And you can do:

>>> x = 1
>>> debug('x + 1')
x + 1 = 2