print("Hi, my name is {0}, and I work at {1}.".format("blank", 36)
why is this ^ ok but this isnt => " blank" + 36
print("Hi, my name is {0}, and I work at {1}.".format("blank", 36)
why is this ^ ok but this isnt => " blank" + 36
As per the docs, the .format
method does "Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument".
So the arguments you provide to .format
get converted to their string representations before being inserted into your string.
You'd have to do it manually to achieve the same by concatentating using the +
operator:
" blank" + str(36)
You cannot concatenate an integer (36
) to a string (" blank"
).
You would need to cast the integer into a string : " blank" + str(36)
, but even if you do that,
print("Hi, my name is {0}, and I work at {1}.".format(" blank" + str(36)))
would not work because the inclusion of both {0}
and {1}
means that two arguments are expected in format()
.
So something that would work is:
print("Hi, my name is {0}, and I work at {1}.".format(" blank" + str(36), "second required argument"))