how to print at same line using end parameter?

Question:

I’d like to print at same line for print statement inside a for loop using end= parameter. not sure which end parameter i can use.

For example, in below, for each time’s print, only need to change str(i) and str(result), everything is the same.

for i in range(10):
   result=i**2
   print('iteration is'+str(i)+' with result of '+str(result))

Thanks

Asked By: roudan

||

Answers:

Use an empty end parameter and go back the length of the previous print

L=0
for i in range(3):
   result=i**2
   my_str = 'iteration is'+str(i)+' with result of '+str(result)
   print('b'*L + my_str, end='')
   L= len(my_str)
Answered By: SiP
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.