What does str.format do? Why should I use it? What's the difference between using
print(a, b)
and
print("{} {}".format(a, b))
Thanks in advance.
What does str.format do? Why should I use it? What's the difference between using
print(a, b)
and
print("{} {}".format(a, b))
Thanks in advance.
Theformat()method takes the arguments or variables which are passed, formats them, and places them in the string wherever the placeholders{}symbol is used.
a = 5
#single formatting
b = "My name is XYZ & my age is just {}"
print (b.format(a))
#multiple formatting
c = "My name is XYZ & my age is just {}. Also I have a sibling who's age is also {}"
print(c.format(a, a))
Output:
My name is XYZ & my age is just 5 My name is XYZ & my age is just {}. Also I have a sibling who's age is also 5