# Clearing the output data

**URL:** <https://discuss.python.org/t/clearing-the-output-data/56480>\
**Category:** Python Help\
**Tags:** help\
**Created:** [June 23, 2024, 3:26pm UTC](https://discuss.python.org/t/clearing-the-output-data/56480 "2024-06-23T15:26:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivanchik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ivanchik/32/20320_2.png) [@Ivanchik](https://discuss.python.org/u/Ivanchik)\
**Post date:** [June 23, 2024, 3:26pm UTC](https://discuss.python.org/t/clearing-the-output-data/56480/1 "2024-06-23T15:26:29Z")

</div>

I am making a clicker.  
When using my program, there is a very long output.  
I need my program to **clear** the old output and **output** a new message.

How can I clear the output?

Just in case, there is my code:

```python
def shop():
  print('"l" для выхода')
  print('-------------Магазин------------')
  print('1. +1 за клик 10$')
  print('2. +5 за клик 50$')
  print('3. Х2 за клик 100$')
  print('4. +1 пассивный доход 80$')
  print('--------------------------------')

money = 0
print('Нажмите любую клавишу для заработка денег')
print('"q" для выхода, "s" для магазина')
while True:
  a = input()
  if a == "q":
    print(f'Итого: {money}$')
    break
  elif a == "s":
    shop()
  else:
    money += 1
  print(f'Баланс: {money}$')

```

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [June 23, 2024, 6:38pm UTC](https://discuss.python.org/t/clearing-the-output-data/56480/2 "2024-06-23T18:38:53Z")

</div>

On Windows, you can clear the console window from the Windows command line with `cls`, so you could run that from Python:

```python
import os
os.system('cls')

```

---

<div class="post-metadata">

**Author:** ![Hichem](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hichem/32/20665_2.png) [@Hichem](https://discuss.python.org/u/Hichem)\
**Post date:** [June 23, 2024, 6:57pm UTC](https://discuss.python.org/t/clearing-the-output-data/56480/3 "2024-06-23T18:57:59Z")

</div>

Here is an updated version of your program that clears the console before each new message. It introduces the `clear_console` function, which uses the `os.system` command to clear the console.

```python
import os

def clear_console():
    # Clear console based on the operating system
    if os.name == 'nt':
        os.system('cls') # For Windows
    else:
        os.system('clear') # For Unix/Linux/Mac

def shop():
    print('"l" для выхода')
    print('-------------Магазин------------')
    print('1. +1 за клик 10$')
    print('2. +5 за клик 50$')
    print('3. Х2 за клик 100$')
    print('4. +1 пассивный доход 80$')
    print('--------------------------------')

money = 0
print('Нажмите любую клавишу для заработка денег')
print('"q" для выхода, "s" для магазина')

while True:
    a = input()
    clear_console() # Clear the console before displaying new message
    if a == "q":
        print(f'Итого: {money}$')
        break
    elif a == "s":
        shop()
    else:
        money += 1
    print(f'Баланс: {money}$')

```

---

<div class="post-metadata">

**Author:** ![Ivanchik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ivanchik/32/20320_2.png) [@Ivanchik](https://discuss.python.org/u/Ivanchik)\
**Post date:** [June 24, 2024, 5:41am UTC](https://discuss.python.org/t/clearing-the-output-data/56480/4 "2024-06-24T05:41:48Z")

</div>

@MRAB @Hichem  
Thanks for the suggestion, but it doesn’t work, probably because I use [Google Colab](https://colab.research.google.com/)

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [June 24, 2024, 9:13am UTC](https://discuss.python.org/t/clearing-the-output-data/56480/5 "2024-06-24T09:13:13Z")

</div>

I don’t understand what “clear the output” should mean in Colab. But I would suggest looking at the Colab documentation first.

There is never anything built in to Python for this, because **Python is not responsible for actually making the text show up**. It only sends data to the “standard output”, which is handled by the environment. On Colab, I guess that puts it into some text field of a web page, in your browser. When you run the code on your own computer, at the command line, the data goes to the terminal. If you use an IDE, the IDE has to put it in some graphical window (maybe it starts a separate terminal process for this).

The Python interpreter doesn’t know anything about where that text will go, therefore it can’t do anything about “clearing” it (or changing the position, etc.) In the terminal, we can use other programs like `cls` (on Windows) or `clear` (on Linux) that can make a system call, to configure the terminal. But every environment will have its own specific solution.

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [June 24, 2024, 3:21pm UTC](https://discuss.python.org/t/clearing-the-output-data/56480/6 "2024-06-24T15:21:45Z")

</div>

You should’ve mentioned that in the post.

A quick search found this:

[How to Clear Python Output Programatically in Google Colaboratory? - Stack Overflow](https://stackoverflow.com/questions/52751413/how-to-clear-python-output-programatically-in-google-colaboratory)

which gives this answer:

```python
from IPython.display import clear_output
clear_output()

```
