loading...

TIL about formatted strings that was introduced in Python 3.6

ashwinimanoj profile image Ashwini Manoj ・1 min read

If you want an easy way to format strings, using formatted strings would be a good way to go! I came across it here when trying to refactor an old code of mine from python2 to python3.

Here's how I was doing string manipulation when I first started off with python:

val = 'This ' + str(a) + ' here multiplied by ' + str(b) + ' = ' + str(a*b)

And now I have learned to do this:

val = f'This {a} here multiplied by {b} = {a*b}'

So much easier to read!

Discussion

pic
Editor guide
Collapse
iceorfiresite profile image
Ice or Fire

F-strings are your friend!

Collapse
ashwinimanoj profile image
Ashwini Manoj Author

Definitely! :D