The module shutil has a method .get_terminal_size that you can use to get the terminal size you're running on.
For example, I stretched out my terminal, made it very short, and ran this code:
>>> import shutil
>>> shutil.get_terminal_size()
os.terminal_size(columns=160, lines=9)
The output that we see is a named tuple, so we can use it in multiple ways:
>>> size = shutil.get_terminal_size()
>>> size.columns
160
>>> width, height = size
>>> width
160
>>> height
9
This method accepts a fallback argument that is returned if Python fails to detect the terminal size (or if there is no terminal associated with the Python interpreter that is running).
This fallback value defaults to (80, 24).
The module os also has a similar method (os.get_terminal_size) but the documentation says the shutil method is the one you should use and os.get_terminal_size is a lower-level method you won't typically need to use.
Get ready for 12 intense days of problem-solving. The βAlgorithm Mastery Bootcampβ starts December 1st and it will feature 24 programming challenges, live analysis sessions, a supportive community of like-minded problem-solvers, and more! Join now and become the Python expert others can rely on.
shutil module, https://docs.python.org/3/library/shutil.html#shutil.get_terminal_size [last accessed 24-05-2024]os module, https://docs.python.org/3/library/os.html#os.get_terminal_size [last accessed 24-05-2024]