Sobes.tech
Junior — Middle

Can you explain what an f-string is in Python?

sobes.tech AI

Answer from AI

f-string (formatted string) in Python is a convenient and readable way to format strings, introduced in Python 3.6. It allows inserting variable values and expressions directly inside the string literal using syntax with the prefix f before the quotes.

Example of usage:

name = "Ivan"
age = 30
print(f"Hello, {name}! You are {age} years old.")

Result:

Hello, Ivan! You are 30 years old.

Any expressions can be used inside the curly braces, for example:

print(f"In 5 years, you will be {age + 5} years old.")

This makes the code more compact and convenient compared to traditional string formatting methods (% or .format()).

Can you explain what an f-string is in Python? — Python - sobes.tech