f-string: the sexiest string

Words
247
Reading
2 min
Listen
Play
7y

If you want to learn more about how to tame python you have come to the right post. Because this post is all about getting sexy with a string :D

Whenever I had a string into which I have to input something like length_rod = 8 I would type

"If you upvote this you have a {} inch rod".format(length_rod).

This pretty much inserts the lengthy rod where you want it. You can also reference them by index so

"I will hit {0} in the face for {1}".format('justin','free').

Reference parameters can be used together with .format exactly in the way you expect it

"Nobody expects {unexpected}".format(unexpected= 'the Spanish inquisition').

But you can imagine if it gets very long then things get complicated and confusing. There is also the %-formating which is something that all basic python tutorials treat but I think that's is only more ugly than .format. Anyway that f-string trumps all of these.

For an f-string everything is super simple. If you first define Sun = "Scum" then you can insert the assigned value to Sun into a string by just typing f"Justin {Sun}". Hence, the name f-string. Besides its simplicity you can also perform operations within a f-string this means that python is totally okay with you running

f"A whale p-nis is about {5*50} cm"

Of course you can also define something and use it within an f-string which is awesome. Anyway, hope that convinced you to use f-strings.

Here is a proper tutorial. ;)

f-string: the sexiest string | Ecency