Bold Formatting in Python Console

how to print bold font when using f-string?

It is the same as the regular string
Try this:

name = 'Yacoub'
age = 23
print(f"This is \033[1mbold\033[0m and my name is \033[1m{name}\033[0m\nI am \033[1m{age}\033[0m years old")

Output:

This is bold and my name is Yacoub

I am 23 years old

How to make input text bold in console?

They are called ANSI escape sequence. Basically you output some special bytes to control how the terminal text looks. Try this:

x = input('Name: \u001b[1m')  # anything from here on will be BOLD

print('\u001b[0m', end='') # anything from here on will be normal
print('Your input is:', x)

\u001b[1m tells the terminal to switch to bold text. \u001b[0m tells it to reset.

This page gives a good introduction to ANSI escape sequence.

Bold text using format() function

Edited answer to include an example of producing bold text in matplotlib and kept my general answer on producing bold text in linux using python:

You'll want to use fontweight='bold' as an additional argument in your function call. For the title of the plot chart below, I increased the font size and made the text bold with the following: plt.title('Info', fontsize=20, fontweight='bold')

import matplotlib
import matplotlib.pyplot as plt
x = [5,2,7]
y = [2,16,4]
plt.plot(x,y)
plt.title('Info', fontsize=20, fontweight='bold')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()

You'll notice that info shows up in bold below:
Sample Image

Here's an alternate way if you want to just bold only a word like in a multiword title:

import matplotlib
import matplotlib.pyplot as plt
x = [5,2,7]
y = [2,16,4]
plt.plot(x,y)
plt.title("This is my " + r"$\bf{" + 'title' + "}$")
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()

You'll notice below that only the word title shows up in bold out of This is the title:
Sample Image

_____________________________________________________________
_____________________________________________________________

_____________________________________________________________

ORIGINAL ANSWER ON PRODUCING BOLD TEXT IN LINUX USING PYTHON:
For producing bold text in linux, here's with and without the format function to first illustrate the concept:

Bold a word in a sentence without format function (linux):

print("The last word in this sentence is",'\033[1m' + 'bolded' + '\033[0m')

Sample Image

Bold a word in a sentence using the format() function (linux):

print("The last word in this sentence is {}bolded{}".format('\033[1m','\033[0m'))

Sample Image

Bold the entire sentence using the format() function (linux):

print("{}This entire sentence is bolded{}".format('\033[1m','\033[0m'))

Sample Image

How to print bold text in python with ```format```?

The same ANSI escape sequences work just fine.

print('\033[1m{:10s}\033[0m'.format('foo'))

How can I make text bold in Python?

You might want to try ANSI escape sequences if your platform allows it.

Crossposting from related - and possible duplicate - SO questions: 1 2

Before you proceed, please read the warnings in the above links to make sure your operating system and Python version will allow you to properly render this.

You can define a class containing all of the ANSI escape sequences you need.

class style:
BOLD = '\033[1m'
END = '\033[0m'

Then print them via concatenated class calls:

print style.BOLD + 'This is my text string.' + style.END

If you don't want to go through the hassle of creating an entire class just to get bold text, you can obviously concatenate them directly instead. However, if your code is open-source and not for personal use, make sure you tell other potential readers what this does!

print '\033[1m' + 'This is my text string.' + '\033[0m'

Does this answer your question?

Edit: JYelton beat me to it. I'm such a slow typer...



Related Topics



Leave a reply



Submit