Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

{}.format function alternative in Python [closed]

Tags:

python

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?

like image 669
Andrew Zaw Avatar asked May 07 '26 20:05

Andrew Zaw


2 Answers

.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.

like image 101
Udi Avatar answered May 10 '26 10:05

Udi


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.')
like image 21
TigerhawkT3 Avatar answered May 10 '26 10:05

TigerhawkT3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!