1

I was just wondering what the bests way to include variables in printed-lines.

For example would it be better to do:

print("The cost will be $" + str(cost) + ".")

or

print("The cost will be ${}.".format(cost))

Thanks, David

P.S. when explaining, note that I am new at programming in general, thanks :D

1 Answers1

2

I would say currently, this is better:

print("The cost will be ${}.".format(cost))

Because no need for converting for string.

Note: if your python version is over 3.5, do print(f'The cost will be${cost}'), because it's the best.

Note there are several other ways that are equally good (with all versions):

print('The cost will be$%s'%cost)

Or (maybe not equally good..):

print("The cost will be $",cost,".",sep='')
U13-Forward
  • 69,221
  • 14
  • 89
  • 114