Print statement

why do we use .format() when we can simply write the variable in the code. for example
a=10
print(“you entered”,a)
print(“you entered {}”.format(a))

Hi ranjan,
It is a matter of our choice what we want to use, there are many approaches for any particular output.
Apart from 2 ways you mentioned there is one more alernative print(“you entered %d"%a )

I personally follow .format() or third approach, the code looks cleaner this way. The first approach you have given would create trouble when you want to merge different types of data for eg - string + int + string + float.

I hope this clear your doubt
Thanks :slight_smile:

I didn’t fully understand that.
Code might be cleaner by other 2 ways but i didn’t understand how it could cause trouble.
Are you saying about this
a=10
b=“name”
c=“math”
d=88.45
print(“My name is:”,b,"\n roll no",a,"\n subject",c,"\n marks",d)

Hi,
Again, it depends on person to person how they actually want to code.

If you look open source codes you will find the format method more often.
Your way is combining both variable + string again and again which is not encouraged.
print("My name is: {} roll no {} subject {} marks {}".format(b,a,c,d))
I personally think this statement looks more clear, since it separates the variables and strings, as i don’t need to create strings multiple times in a single print statement.

You can choose your way, it won’t make much difference.