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