Can we provide any input type to the format() method?

Question

In the context of this exercise, can we provide any input type to the format() method?

Answer

Yes, you can provide almost any type of input to the format() method, including strings, numerical values, booleans, and objects. You can also provide expressions, like 1 + 4, which will be evaluated and then formatted into the string.

Example

a = 10
b = 20

print("{} + {} = {}".format(a, b, a+b))
# 10 + 20 = 30

print("{}".format(a < b))
# True
5 Likes

Is it possible to call a function inside .format() method?

8 Likes

Great question!
I think so, please see below:

def function():
  return 'function called!'

print('{}'.format(function()))
# function called!
29 Likes