How to Overwrite the Previous Print to Stdout in Python?

Summary: The most straightforward way to overwrite the previous print to stdout is to set the carriage return (‘\r’) character within the print statement as print(string, end = “\r”). This returns the next stdout line to the beginning of the line without proceeding to the next line. Problem Formulation Problem Definition: How will you overwrite … Read more

How to Print Colored Text in Python?

Summary: To print colored text in Python, you can use: The simple_color package, The ‘\033[1m’ ANSI escape-sequence, The termcolor module, The coloroma package, The colored library, The prompt_toolkit package. A simple, no-library way to print bolded text in Python is to enclose a given string s in the special escape sequence like so: print(“\033[38;5;4m”). We’ll … Read more

How to Add Time Onto a Datetime Object in Python

[toc] Problem: Given a datetime.datetime object in Python, is there a way to add time to the given datetime object? Related Question on StackOverflow: Discussion: Adding time to a datetime object in Python should lead to a new datetime object with the updated time information. For example, if you add 5 hours to a datetime … Read more

How to Print Bold Text in Python?

A simple, no-library way to print bolded text in Python is to enclose a given string s in the special escape sequence like so: print(“\033[1m” + s + “\033[0m”). We’ll discuss this most Pythonic solution in Method 2 in this article. You can change your text to bold, italic, and underlined in Python. Not only … Read more

How to Suppress Scientific Notation in Python

[toc] Summary: Use the string literal syntax f”{number:.nf}” to suppress the scientific notation of a number to its floating-point representation. Problem Formulation: How will you suppress a number represented in scientific notation by default to a floating-point value? Note: Generally, Python represents huge floating-point numbers or very small floating-point numbers in their scientific form. Scientific … Read more

How to Search for Specific Files Only in Subdirectories in Python?

[toc] Problem Formulation: Let’s say we have a directory containing other subdirectories which further contain files. How do we search for a specific file in the subdirectories in our Python script? Scenario: We have a parent folder (Parent) with child folders (child_1, child_2, and child_3). There are files in the parent directory/folder as well as … Read more

Extract File Name From the Path, No Matter What the os/path Format

Summary: os.path.basename(path) enables us to get the file name from the path, no matter what the os/path format. Another workaround is to use the ntpath module, which is equivalent to os.path. ✨Problem: How to extract the filename from a path, no matter what the operating system or path format is? For example, let’s suppose that … Read more

How to Measure Elapsed Time in Python?

Summary: You can evaluate the execution time of your code by saving the timestamps using time.time() at the beginning and the end of your code. Then, you can find the difference between the start and the end timestamps that results in the total execution time. [toc] Problem: Given a Python program; how will you measure … Read more

Installing Specific Package Versions With PIP

Summary: There are primarily two ways of installing a package with a specific version using pip – pip install -Iv package-name == version  pip install -v package-name == version Problem Statement: How to install a specific package version with pip in Python? In Python, pip is a command and tool used to install, update and … Read more