Kodeclik Blog


How to bold text in Python

When working with text in Python, you might want to emphasize certain words or phrases to make them stand out. One way to achieve this effect is by printing bold text. While Python doesn't have built-in support for formatting text in this way, you can use control characters to accomplish the task. In this blog post, we'll explore how to print bold text in Python using control characters.

What are control characters?

Control characters are special characters that don't have a visible representation but instead control various aspects of text formatting and display. In Python, you can use control characters, specifically the ANSI escape codes, to change the style of your text. These codes are sequences of characters that, when printed to the terminal, modify the text's appearance.
To make text bold, we'll use the ANSI escape code for bold text, which is \033[1m. This code tells the terminal to switch to bold text mode. To turn off bold text, you can use the ANSI escape code \033[0m.

Printing Bold Text in Python

Let's start by printing a simple message in bold text using control characters:
In this example, we use \033[1m before the text we wish to make bold and \033[0m after it to reset the text style. The message (or string) that we would like to print is thus in between these control characters.
When you run this code, you'll see "This text is bold!" displayed in bold in your terminal.

Adding Bold Text to Variables

You can also add bold text in the values we set to variables and include them in your output. Here's an example:
In this example, we create a variable bold_text that contains the bold text with the appropriate control characters. We then concatenate it with other text to create a more complex output.

Creating Functions for Bold Text

If you plan to use bold text frequently, it's a good idea to create a function to simplify the process. Here's a basic function that you can use:
This function, bold_text(), takes a string as input and returns the string formatted in bold.

How to make bold alternate characters in a string

In order to alternate between setting characters in a string to bold, you can use control characters in a plain text document. Here is a Python program:
In the above code, we iterate through the characters in the original_string using enumerate to keep track of the character's index. If the index is even (0-based index), we enclose the character in control characters (\033[1m for bold start and \033[0m for formatting end) to make it bold. If the index is odd, we simply add the character without any formatting. This will result in alternate characters in the string being displayed in bold, while the others remain in their regular format.
So in summary, using control characters, specifically ANSI escape codes, you can easily print bold text in Python. This simple technique allows you to add emphasis to your output and improve the readability of your text-based applications. Whether you're creating command-line tools or enhancing the user experience in your terminal, knowing how to print bold text using control characters is a valuable skill.
If you liked this blogpost, checkout our blogpost on how to pretty print JSON data structures in Python.
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.
  • ABOUT

Copyright @ Kodeclik 2024. All rights reserved.