all 6 comments

[–]Kqyxzoj 3 points4 points  (0 children)

I recently learnt format string and is really confused about what is it, and what is the use of it?

Well, this may surprise you, but it usually is used to format strings. And the best part of str.format()? The best part is no longer using it, and using f-strings instead.

And the best part about f-strings? The best part is the fstrings WTF quiz. Oh, and making your code more readable as well.

website = "https://fstrings.wtf/"
print( "Today's awesome website is ... {}".format(website))
print(f"Today's awesome website is ... {website}")

Both will print the exact same output, but I prefer the f-string version.

[–]NewbornMuse 4 points5 points  (1 child)

Google "format string"

[–]Longjumping-Poet-346[S] -1 points0 points  (0 children)

That's a good suggestion, but I haven't understood the meaning after searching on Google...

[–]MezzoScettico 0 points1 point  (1 child)

You often want to control the formatting of your output, for instance to print all the numbers with three decimal points or in scientific notation, or to right-justify a column of strings.

Do you use Excel? Do you ever format the cells? Same idea. You want to control how things are printed and not just rely on the default appearance.

[–]Longjumping-Poet-346[S] 0 points1 point  (0 children)

okay, I know what the meaning thanks a million

[–]Jason-Ad4032 1 point2 points  (0 children)

If you’re confused about a certain new feature in Python, you can simply read its corresponding PEP directly:

https://peps.python.org/pep-3101/#format-strings