Skip to content

Python print(f-string)

  • by

To print Python print(f-string), you need to enclose each of the variable names inside a set of curly braces.

f"This is an f-string {var_name} and {var_name}."

Python print(f-string)

Simple example code Print Variables using Python f-Strings. Just specify the names of the variables inside a set of curly braces {}. All variable names will be replaced with their respective values.

name = "John"
age = 32
print(f"I'm  {name} age {age}.")

Output:

Python print(f-string)

Below is a table detailing the usage of f-strings (formatted string literals) in Python.

FeatureDescriptionExample
Basic UsageEmbed expressions inside string literals, using curly braces {}name = "Alice" <br> print(f"Hello, {name}!") <br> Output: Hello, Alice!
VariablesDirectly insert variablesage = 30 <br> print(f"Age: {age}") <br> Output: Age: 30
ExpressionsEvaluate expressions within the bracesa = 5; b = 10 <br> print(f"Sum: {a + b}") <br> Output: Sum: 15
Calling FunctionsCall functions inside the bracesdef greet(): return "Hello" <br> print(f"{greet()}, World!") <br> Output: Hello, World!
Formatting NumbersFormat numbers for better readabilityvalue = 1234.5678 <br> print(f"Formatted: {value:.2f}") <br> Output: Formatted: 1234.57
Padding and AlignmentAlign text with spaces or specific characterstext = "Hi" <br> print(f"{text:<10}") <br> Output: Hi
Multiline f-stringsSpread f-strings across multiple lines with triple quotesname = "Alice"; age = 30 <br> print(f"""Name: {name} Age: {age}""") <br> Output: Name: Alice Age: 30
Braces within f-stringsUse double braces {{ and }} to include literal braces in the outputprint(f"{{}}") <br> Output: {}
Nested f-stringsUse f-strings within other f-stringslevel = 5 <br> print(f"Level: {f'{level}'}") <br> Output: Level: 5
Using DictionariesAccess dictionary values within f-stringsdata = {"key": "value"} <br> print(f"Key: {data['key']}") <br> Output: Key: value
Conditional ExpressionsInclude conditional expressions directlystatus = True <br> print(f"Status: {'Active' if status else 'Inactive'}") <br> Output: Status: Active
Importing ModulesUse functions from imported modulesimport math <br> print(f"Pi: {math.pi:.2f}") <br> Output: Pi: 3.14
Accessing Object AttributesAccess object attributes within f-stringsclass Person: <br> def __init__(self, name): self.name = name <br> p = Person("Alice") <br> print(f"Name: {p.name}") <br> Output: Name: Alice
Date FormattingFormat dates within f-stringsfrom datetime import datetime <br> now = datetime.now() <br> print(f"Date: {now:%Y-%m-%d}") <br> Output: Date: 2024-06-11
Escape SequencesUse backslashes for escape sequences within f-stringsprint(f"Newline: \\n Tab: \\t") <br> Output: Newline: \n Tab: \t

Do comment if you have any doubts or suggestions on this Python string formatting topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading