MWZ

MINDWAREZONE

f-Strings

f-strings (formatted string literals) were introduced in Python 3.6 to make string formatting easier and more readable. They allow variables and expressions to be directly embedded inside strings using curly braces {}.

Syntax

f"text {variable}"
            

Example 1

name = "Alice"

print(f"Welcome {name}")
            

Output

Welcome Alice
            

Example 2

price = 999
print(f"Price: ₹{price}")
            

Output

Price: ₹999
            

Example 3

a = 10
b = 20

print(f"Sum = {a + b}")
            

Output

Sum = 30
            

Why Use f-Strings?

  • Easy to read
  • Faster than format()
  • Supports expressions directly