Really quick and simple question.
I've come to love the use of {}.format, even if im not formatting the variable itself, as in:
print("Welcome to {}, {} {}!".format(location,firstname,lastname))
as it means I don't have put commas in between strings and variables, and can directly control spacing within the string. However, if I still do it even with just one variable like
print("I am at {} right now.".format(location))
Is there anything wrong with using format like this?
Is there a better way to do the same thing?
.format() is OK, even with one variable. In python 3.6 you can also use:
print(f"I am at {location} right now.")
See discussion in PEP 0498 for more info.
If all you're doing is printing, and the objects you're printing are all separated by the same strings (like a space), you can just pass them as multiple arguments to print.
print('I am at', location, 'right now.')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With