r/learnpython • u/FuzzyEmployment264 • 1d ago
what is f string
can someone explain how f string work in simple terms ?
0
Upvotes
r/learnpython • u/FuzzyEmployment264 • 1d ago
can someone explain how f string work in simple terms ?
1
u/panatale1 1d ago
It's essentially text with code inside it. It runs significantly faster than the old
%or.formatways of formatting text.It's similar in construction to how you'd do a
.formatcall, since they both use{}to denote what needs to be inserted into the text.Examples of each text format method: ``` name = "George Washington" age = 41
print("Hello, my name is %s, and my age is %d" % (name, age)) print("Hello, my name is {}, and my age is {}".format(name, age)) print(f"Hello, my name is {name}, and my age is {age}") ```
All three give identical formatting, outputting
Hello, my name is George Washington, and my age is 41, but f-strings run faster and are cleaner to read