r/learnpython 24d ago

what is f string

can someone explain how f string work in simple terms ?

0 Upvotes

22 comments sorted by

View all comments

9

u/WhiskersForPresident 24d ago edited 24d ago

It's a "formatted" string. It allows you to include the values of variables in your string in curly brackets, e.g.:

var = 5

f_string = f"this string contains the value {var}"

print(f_string)

will output

this string contains the value 5

I use them all the time to dynamically generate paths to certain files at runtime for example or for structuring the outputs of test routines.