f-string in Python

The syntax starting with f" is used for f-strings in Python. F-strings, or formatted string literals, were introduced in Python 3.6 to embed expressions inside string literals, using curly braces {}. The Expressions can be arithmetic, function calls, attribute access, indexing, etc.:

Explanation of the Syntax:

Example:

Python

# Create some variables
name = "Ashish"
age = 25

description = f"My name is {name}, and I am {age +1} years old."
print(description)
# My name is Ashish, and I am 26 years old.

How it Works: • The f-string f"My name is {name}, and I am {age +1} years old." automatically replaces the variables name, age with their values and constructs the final string “My name is Ashish, and I am 26 years old.”.

Multiline f-strings Works with triple quotes:

Python

# Create some variables
name = "Ashish"
age = 25

description = f"""My name is {name}.
I am {age +1} years old."""
print(description)
# My name is Ashish.
# I am 26 years old.