191. Python’s F-strings Internals

1

Basic f-string Syntax

  • The simplest way to use an f-string.

Copy

name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")

🔍 Under the hood:

  • At runtime, Python replaces {name} and {age} with their values and evaluates them dynamically.


2

f-strings Use str.format() Internally

  • f-strings are syntactic sugar for str.format().

Copy

name = "Bob"
age = 30

# Equivalent f-string and str.format()
f_string = f"My name is {name} and I am {age} years old."
format_string = "My name is {} and I am {} years old.".format(name, age)

print(f_string)  
print(format_string)

🔍 Under the hood:

  • f"My name is {name} and I am {age} years old." is roughly compiled to:

    Copy


3. f-strings Evaluate Expressions

  • f-strings can evaluate any expression inside {}.

Copy

🔍 Under the hood:

  • The expressions {x + y} and {x / 2:.2f} are evaluated before string formatting.


4. f-strings Use __format__() Internally

  • Python calls the __format__() method on objects inside f-strings.

Copy

🔍 Under the hood:

  • f"Hello, {p:upper}!" calls p.__format__("upper"), which customizes the output.


5. f-strings are Faster than str.format()

  • Since f-strings are evaluated at compile-time, they are faster than format().

Copy

🔍 Under the hood:

  • f-strings are compiled to efficient bytecode, avoiding extra function calls like format().


Last updated