F strings are just a nice way to insert variables into a template string. Yes, there are other ways - but f strings are usually the best. What way would you use instead? Answer from fiddle_n on reddit.com
🌐
W3Schools
w3schools.com › python › python_string_formatting.asp
Python String Formatting
You can perform Python operations inside the placeholders. ... price = 59 tax = 0.25 txt = f"The price is {price + (price * tax)} dollars" print(txt) Try it Yourself »
🌐
GeeksforGeeks
geeksforgeeks.org › python › formatted-string-literals-f-strings-python
f-strings in Python - GeeksforGeeks
May 16, 2026 - Example 3: This example evaluates an expression directly inside an f-string. ... Physics = 78 Chemistry = 56 Biology = 85 print(f"Alex got total marks {Physics + Chemistry + Biology} out of 300")
🌐
Bentley
cissandbox.bentley.edu › sandbox › wp-content › uploads › 2022-02-10-Documentation-on-f-strings-Updated.pdf pdf
Updated 2022 A Guide to Formatting with f-strings in Python
the f-string to help delineate the spacing. That number after the ":"will cause that field to be · that number of characters wide. The first line in the print() statement using f-strings is an
Top answer
1 of 7
150

The f means Formatted string literals and it's new in Python 3.6.


A formatted string literal or f-string is a string literal that is prefixed with f or F. These strings may contain replacement fields, which are expressions delimited by curly braces {}. While other string literals always have a constant value, formatted strings are really expressions evaluated at run time.


Some examples of formatted string literals:

>>> name = "Fred"
>>> f"He said his name is {name}."
"He said his name is Fred."

>>> name = "Fred"
>>> f"He said his name is {name!r}."
"He said his name is Fred."

>>> f"He said his name is {repr(name)}." # repr() is equivalent to !r
"He said his name is Fred."

>>> width = 10
>>> precision = 4
>>> value = decimal.Decimal("12.34567")
>>> f"result: {value:{width}.{precision}}" # nested fields
result: 12.35

>>> today = datetime(year=2023, month=1, day=27)
>>> f"{today:%B %d, %Y}" # using date format specifier
January 27, 2023

>>> number = 1024
>>> f"{number:#0x}" # using integer format specifier
0x400
2 of 7
63

In Python 3.6, the f-string, formatted string literal, was introduced(PEP 498). In short, it is a way to format your string that is more readable and fast.

Example:

agent_name = 'James Bond'
kill_count = 9


# old ways
print("%s has killed %d enemies" % (agent_name,kill_count))

print('{} has killed {} enemies'.format(agent_name,kill_count))
print('{name} has killed {kill} enemies'.format(name=agent_name,kill=kill_count))
    

# f-strings way
print(f'{agent_name} has killed {kill_count} enemies')

The f or F in front of strings tell Python to look at the values , expressions or instance inside {} and substitute them with the variables values or results if exists. The best thing about f-formatting is that you can do cool stuff in {}, e.g. {kill_count * 100}.

You can use it to debug using print e.g.

print(f'the {agent_name=}.')
# the agent_name='James Bond'

Formatting, such as zero-padding, float and percentage rounding is made easier:

print(f'{agent_name} shoot with {9/11 : .2f} or {9/11: .1%} accuracy')
# James Bond shoot with  0.82 or  81.8% accuracy 

Even cooler is the ability to nest and format. Example date


from datetime import datetime

lookup = {
    '1': 'st',
    '21': 'st',
    '31': 'st',
    '2': 'nd',
    '22': 'nd',
    '3': 'rd',
    '23': 'rd'
}

dato = datetime.now()

print(f"{dato: %B %-d{lookup.get(f'{dato:%-d}', 'th')} %Y}")

# April 23rd 2022

Pretty formatting is also easier

tax = 1234

print(f'{tax:,}') # separate 1k \w comma
# 1,234

print(f'{tax:,.2f}') # all two decimals 
# 1,234.00

print(f'{tax:~>8}') # pad left with ~ to fill eight characters or < other direction
# ~~~~1234

print(f'{tax:~^20}') # centre and pad
# ~~~~~~~~1234~~~~~~~~

The __format__ allows you to funk with this feature. Example


class Money:
    
    def __init__(self, value, currency='€'):
        self.currency = currency
        self.value = value
        
    def __repr__(self):
        return f'Money(value={self.value}, currency={self.currency})'
        
    def __format__(self, *_):
        
        return f"{self.currency}{float(self.value):.2f}"
        
        
tax = 12.3446
money = Money(tax, currency='$')

print(f'{money}')
# $12.34

print(money)
# Money(value=12.3446, currency=$)

There is much more. Readings:

  • PEP 498 Literal String Interpolation
  • Python String Formatting
🌐
Python documentation
docs.python.org › 3 › tutorial › inputoutput.html
7. Input and Output — Python 3.14.6 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 ...
Find elsewhere
🌐
Mimo
mimo.org › glossary › python › formatted-strings
Python Formatted Strings / f-string formatting Guide
The :.2f inside the curly braces ... f-strings use a straightforward syntax. To create an f-string, prefix a string literal with f and include any Python expression inside curly braces ({ and }):...
🌐
GeeksforGeeks
geeksforgeeks.org › python › string-alignment-in-python-f-string
String Alignment in Python f-string - GeeksforGeeks
July 15, 2025 - Each alignment specifier is followed by a width number, which determines how much space is reserved for the string. ... Explanation: < specifier left-aligns "Hello", so spaces are added to the right. The > specifier right-aligns "Hello", meaning spaces are added before it. The ^ specifier centers "Hello", adding an equal number of spaces on both sides. ... a = "Alice" b = 25 # Left-aligned (<), Right-aligned (>), and Center-aligned (^) print(f"Name: {a:<10} Age: {b:>5} ") print(f"Name: {a:^10} Age: {b:^5} ")
🌐
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!
🌐
Analytics Vidhya
analyticsvidhya.com › home › mastering f-strings in python: the ultimate guide to string formatting
Mastering f-strings in Python: The Ultimate Guide to String Formatting
March 13, 2024 - In this example, the variables name and age are inserted directly into the string using curly braces inside the f-string. When you run this code, it will print: “My name is Alice, and I am 30 years old.” F-strings allow for effortless string formatting with variables, making your code more concise and readable.
🌐
DataCamp
datacamp.com › tutorial › python-f-string
Python f-string: A Complete Guide | DataCamp
December 3, 2024 - Unlike traditional string formatting methods, f-strings provide a more straightforward and readable way to embed Python expressions directly within string literals. Consider this common scenario where you need to format a simple greeting: name = "Alice" age = 25 # Using % operator (old style) print("My name is %s and I'm %d years old" % (name, age)) # Using str.format() print("My name is {} and I'm {} years old".format(name, age)) # Using f-string print(f"My name is {name} and I'm {age} years old")
🌐
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.6 documentation
The Python parser strips indentation from multi-line string literals when they serve as module, class, or function docstrings. ... >>> def my_function(): ... """Do nothing, but document it. ... ... No, really, it doesn't do anything: ... ... >>> my_function() ... >>> ... """ ... pass ... >>> print(...
🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Use f-strings in Python | note.nkmk.me
May 18, 2023 - Convert between isoformat string and datetime in Python · To include braces { and } in f-strings, double them like {{ and }}. Note that the backslash \ cannot be used. n = 123 print(f'{{}}-{n}-{{{n}}}') # {}-123-{123} source: f_strings.py · Similar to the format() method, f-strings allow replacement fields within other replacement fields.
🌐
DEV Community
dev.to › erictleung › print-fixed-fields-using-f-strings-in-python-26ng
Print fixed fields using f-strings in Python - DEV Community
August 20, 2020 - To create an f-string, you must be using Python 3 and type out something like this. greet = 'Hello' print(f"{greet}, World!") # Hello, World
🌐
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.
🌐
Real Python
realpython.com › python-string-formatting
Python String Formatting: Available Tools and Their Features – Real Python
December 2, 2024 - The different types of string formatting in Python include f-strings for embedding expressions inside string literals, the .format() method for creating string templates and filling them with values, and the modulo operator (%), an older method used in legacy code similar to C’s printf() function.
🌐
CodeWithHarry
codewithharry.com › blogpost › python-cheatsheet
Python CheatSheet | Blog | CodeWithHarry
Basic syntax from the Python ... print("Hi my name is: ", var1) You can also use f-strings for cleaner output formatting: name = "Shruti" print(f"Hi my name is: {name}") The input function is used to take input as a string from the user: var1 = input("Enter your name: ...
🌐
Python
peps.python.org › pep-0498
PEP 498 – Literal String Interpolation | peps.python.org
>>> for x in (32, 100, 'fifty'): ... print(f'x = {x:+3}') ... 'x = +32' 'x = +100' Traceback (most recent call last): File "<stdin>", line 2, in <module> ValueError: Sign not allowed in string format specifier
🌐
Python.org
discuss.python.org › python help
How to use f string format without changing content of string? - Python Help - Discussions on Python.org
June 15, 2022 - def engine(current_money=10.0): # Instruction print( f""" --------------------------------------------------------------------------- | You start with ${current_money} …