DEV Community

Ashwini Manoj
Ashwini Manoj

Posted on

TIL about formatted strings that was introduced in Python 3.6

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!

Top comments (2)

Collapse
 
iceorfiresite profile image
Ice or Fire

F-strings are your friend!

Collapse
 
ashwinimanoj profile image
Ashwini Manoj

Definitely! :D