Is there a way to only clear part of the screen in python?

I’m new to coding and am using VS Code to learn Python.
Is there a way to only clear a section or part of the screen?
I know how to clear the whole screen, but I want to leave part of it alone.

I want to have the program title and initial instructions printed at the top of the screen, with some user input instructions lower down. I want the user input instructions to change in response to the input, while leaving the top alone.

Is there a way to do this, without a while loop constantly re-printing the whole screen each time?
I’ve searched all over and can’t seem to find a solution!

I’ve tried:

import os
os.system('cls')

but this clears the whole screen and I only want to clear part of it, say from line 5 onwards.

New contributor

Matthew Keeble is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

7

First of all: if you’ve learned Python’s “print” and “input” and now intend to make more sophisticated programs, you might want to learn how to create a proper GUI with tkinter, or a local web app with Flask. Proper terminal based programs can be hard to master.

That said, you would be better using a proper terminal library. The “way” you know to clear the screen, though very spread in the internet is more or less infamous: it requires running an entire program, besides your own Python code – just to do the same that printing a four character sequence (using proper control characters) do.

“ncurses” is one such lib which comes pre-installed with Python – but it won’t work on the windows cmd (it might work if you use the “windows terminal” app, though).

There is a version of curses for Windows on Pypi, and also there are textual, terminedia and other libs which can give you more control over the terminal without having to use raw ctrl characters – known as ANSI sequences.

If you are on Linux, Mac, or a proper terminal program in Windows,
These work by printing sequences starting by "<ESC>[" and ending with a command. The special character “ESC” is printed using its codepoint "x1b" (or some prefer the "33" octal form).

So, print("x1b[2J", end="", flush=True) will erase the entire screen (without running another entire process). And changing the “2” in this sequence to “0” or “1” will erase from the cursor position to the end of the screen, or from the cursor position to the start of the screen. – a Partial erase, as you want. But you have to combine that with cursor movement sequences.

So, to move the cursor up 8 lines, and erase from there to the end of the screen you can do, for example: print("x1b[8Ax1b[0J", end="", flush=True)

(note that the named “end” and “flush” arguments to print will avoid it placing an extra new-line which would forward the cursor one line bellow where it’s positioned)

Also, if you are on Windows and these sequences are not working, one way is to use the colorama package – it emulates ANSI chars even on windows cmd.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật