.format() in Python

Hemanth Raju
2 min readMar 25, 2020

--

String Formatting using str.format()

Hi All,this is small note about .format() function in Python.Many of us come across so many functions in python which makes our code more efficient and writing the code in single line.The .format() is another simple function which is so useful.It is easy to implement also.Let us see an example,

image 1

In image 1, we have seen that we have to convert number to string otherwise it will throw an error.It is a simple statement so we can easily convert to string.What happens when we have a file with so many numbers that we need to convert to string?It is a tedious work.

So,now .format() function comes into picture.Now,let us see how we can use .format() function.

image 2

In the image 2,we can see that we can just convert any data type into string by using curly braces({}).There are still so many uses with .format() function.Try it out.

--

--

No responses yet