How to do a backspace in python

8k views Asked by At

I'm trying to figure out how to print a one line string while using a for loop. If there are other ways that you know of, I would appreciate the help. Thank you. Also, try edit off my code!

times = int(input("Enter a number: "))
print(times)
a = 0
for i in range(times+1):
    print("*"*i)
    a += i
print("Total stars: ")
print(a)
print("Equation: ")
for e in range(1,times+1):
    print(e)
    if e != times:
        print("+")
    else:
        pass

Out:

Enter a number: 5
*
**
***
****
*****
Equation:
1
+
2
+
3
+
4
+
5

How do I make the equation in just one single line like this:

1+2+3+4+5
4

There are 4 answers

0
user2570465 On BEST ANSWER

I don't think you can do a "backspace" after you've printed. At least erasing from the terminal isn't going to be done very easily. But you can build the string before you print it:

times = int(input("Enter a number: "))
print(times)
a = 0
for i in range(times+1):
    print("*"*i)
    a += i
print("Total stars: ")
print(a)
print("Equation: ")
equation_string = ""
for e in range(1,times+1):
    equation_string += str(e)
    if e != times:
        equation_string += "+"
    else:
        pass
print(equation_string)

Basically, what happens is you store the temporary equation in equation_str so it's built like this:

1
1+
1+2
1+2+
...

And then you print equation_str once it's completely built. The output of the modified program is this

Enter a number: 5
5

*
**
***
****
*****
Total stars: 
15
Equation: 
1+2+3+4+5

Feel free to post a comment if anything is unclear.

0
Andrew Guy On

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)

0
NaN On

Too long for a comment, so I will post here. The formatting options of python can come into good use, if you have a sequence you wish to format and print. Consider the following...

>>> num = 5     # number of numbers to generate
>>> n = num-1   # one less used in generating format string
>>> times = [i for i in range(1,num+1)]  # generate your numbers
>>> ("{}+"*n + "{}=").format(*times)     # format your outputs
'1+2+3+4+5='

So although this doesn't answer your question, you can see that list comprehensions can be brought into play to generate your list of values, which can then be used in the format generation. The format string can also be produced with a l.c. but it gets pretty messy when you want to incorporate string elements like the + and = as shown in the above example.

0
Diomedea On

I think you are looking for the end parameter for the print function - i.e. print(e, end='') which prints each value of e as it arrives followed by no space or newline.