Skip to main content
added 95 characters in body
Source Link
Andrew Guy
  • 10.1k
  • 3
  • 32
  • 42

Instead of your original for loop to print each number, try this:

output = '+'.join([str(i) for i in range(1, times + 1)])
print(output)

Explanation:

[str(i) for i in range(1, times + 1)] is a list comprehension that returns a list of all your numbers, converted to strings so that we can print them.

'+'.join(...) joins each element of your list, with a + in between each element.

Alternatively:

If you want a simple modification to your original code, you can simply suppress the newline from each print statement with the keyword paramater end, and set this to an empty string:

print(e, end='')

(Note that I am addressed the implied question, not the 'how do I do a backspace' question)

Instead of your original for loop to print each number, try this:

output = '+'.join([str(i) for i in range(1, times + 1)])
print(output)

Explanation:

[str(i) for i in range(1, times + 1)] is a list comprehension that returns a list of all your numbers, converted to strings so that we can print them.

'+'.join(...) joins each element of your list, with a + in between each element.

Alternatively:

If you want a simple modification to your original code, you can simply suppress the newline from each print statement with the keyword paramater end, and set this to an empty string:

print(e, end='')

Instead of your original for loop to print each number, try this:

output = '+'.join([str(i) for i in range(1, times + 1)])
print(output)

Explanation:

[str(i) for i in range(1, times + 1)] is a list comprehension that returns a list of all your numbers, converted to strings so that we can print them.

'+'.join(...) joins each element of your list, with a + in between each element.

Alternatively:

If you want a simple modification to your original code, you can simply suppress the newline from each print statement with the keyword paramater end, and set this to an empty string:

print(e, end='')

(Note that I am addressed the implied question, not the 'how do I do a backspace' question)

Source Link
Andrew Guy
  • 10.1k
  • 3
  • 32
  • 42

Instead of your original for loop to print each number, try this:

output = '+'.join([str(i) for i in range(1, times + 1)])
print(output)

Explanation:

[str(i) for i in range(1, times + 1)] is a list comprehension that returns a list of all your numbers, converted to strings so that we can print them.

'+'.join(...) joins each element of your list, with a + in between each element.

Alternatively:

If you want a simple modification to your original code, you can simply suppress the newline from each print statement with the keyword paramater end, and set this to an empty string:

print(e, end='')