Monday, 15 August 2011

python - How to use one print statement yet still print on multiple lines -



python - How to use one print statement yet still print on multiple lines -

is there way can utilize 1 print statement, yet still accomplish same effect in code below? have attempted end statements either 1 don't work in situation or, using incorrectly:

print ('deposit: ' + str(deposit)) print ('withdrawl: ' + str(withdrawl)) print ('available amount: ' + str((deposit + withdrawl)//1))

yes, can utilize \n insert newline:

print('deposit: {}\nwithdrawl: {}\navailable amount: {}'.format( deposit, withdrawl, (deposit + withdrawl) // 1))

it's not better, though. imho using separate print() statements here more readable.

you create improve string concatenation:

print(('deposit: {}\n' + 'withdrawl: {}\n' + 'available amount: {}').format(deposit, withdrawl, (deposit + withdrawl) // 1)

which, again, not improve imho.

i used format improve readability; removed need manual str calls, , more readable (it can lot more, see link).

i have attempted end statements either 1 dont work in situation or, using incorrectly

i assume used print('foo', 'bar', end='\n'), won't work, because end appended end end of arguments; sep parameter printed between arguments (this defaults space). want do, is: print('foo', 'bar', sep='\n')

the downside of this, need 3 .format calls, or maintain "ugly" string concatenations.

python string output

No comments:

Post a Comment