What are f-strings?

0297

F-strings, or formatted string literals, are a way to embed expressions inside string literals in Python. They are prefixed with the letter f or F and allow you to include variables and expressions directly within curly braces {}.

Key Features:

  • Easy to Read: F-strings make it clear where variables are being inserted.
  • Supports Expressions: You can include not just variables but also expressions (e.g., calculations) inside the curly braces.

Example:

name = "Alice"
age = 30
greeting = f"My name is {name} and I am {age} years old."
print(greeting)  # Output: My name is Alice and I am 30 years old.

F-strings were introduced in Python 3.6, making string formatting more straightforward and efficient. If you want to learn more, consider exploring Python's official documentation on string formatting!

0 Comments

no data
Be the first to share your comment!