formated text in python? (bold, italic, etc.) Tricker red = '\033[91m' green = '\033[92m' blue = '\033[94m' bold = '\033[1m' italics = '\033[3m' underline = '\033[4m' end = '\033[0m' print (red + underline + 'Test!... Test! ' + end) something like this?
Type the keyboard shortcut: CTRL+B.
The command fontweight='bold' can be used to make a textbox or label in figure bold.
Text can be styled using a attribute tag: This sentence makes a {bold True}bold{bold False} statement. The attribute tag consists of the attribute name (in this example, bold) followed by a Python bool, int, float, string, tuple or list.
class color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
print(color.BOLD + 'Hello World !' + color.END)
Use this:
print '\033[1m' + 'Hello'
And to change back to normal:
print '\033[0m'
This page is a good reference for printing in colors and font-weights. Go to the section that says 'Set graphics mode:'
And note this won't work on all operating systems but you don't need any modules.
You can use termcolor for this:
sudo pip install termcolor
To print a colored bold:
from termcolor import colored
print(colored('Hello', 'green', attrs=['bold']))
For more information, see termcolor on PyPi.
simple-colors is another package with similar syntax:
from simple_colors import *
print(green('Hello', ['bold'])
The equivalent in colorama may be Style.BRIGHT
.
In straight-up computer programming, there is no such thing as "printing bold text". Let's back up a bit and understand that your text is a string of bytes and bytes are just bundles of bits. To the computer, here's your "hello" text, in binary.
0110100001100101011011000110110001101111
Each one or zero is a bit. Every eight bits is a byte. Every byte is, in a string like that in Python 2.x, one letter/number/punctuation item (called a character). So for example:
01101000 01100101 01101100 01101100 01101111
h e l l o
The computer translates those bits into letters, but in a traditional string (called an ASCII string), there is nothing to indicate bold text. In a Unicode string, which works a little differently, the computer can support international language characters, like Chinese ones, but again, there's nothing to say that some text is bold and some text is not. There's also no explicit font, text size, etc.
In the case of printing HTML, you're still outputting a string. But the computer program reading that string (a web browser) is programmed to interpret text like this is <b>bold</b>
as "this is bold" when it converts your string of letters into pixels on the screen. If all text were WYSIWYG, the need for HTML itself would be mitigated -- you would just select text in your editor and bold it instead of typing out the HTML.
Other programs use different systems -- a lot of answers explained a completely different system for printing bold text on terminals. I'm glad you found out how to do what you want to do, but at some point, you'll want to understand how strings and memory work.
This depends if you're using linux/unix:
>>> start = "\033[1m"
>>> end = "\033[0;0m"
>>> print "The" + start + "text" + end + " is bold."
The text is bold.
The word text
should be bold.
Check out colorama. It doesn't necessarily help with bolding... but you can do colorized output on both Windows and Linux, and control the brightness:
from colorama import *
init(autoreset=True)
print Fore.RED + 'some red text'
print Style.BRIGHT + Fore.RED + 'some bright red text'
There is a very useful module for formatting text (bold, underline, colors..) in Python. It uses curses
lib but it's very straight-forward to use.
An example:
from terminal import render
print render('%(BG_YELLOW)s%(RED)s%(BOLD)sHey this is a test%(NORMAL)s')
print render('%(BG_GREEN)s%(RED)s%(UNDERLINE)sAnother test%(NORMAL)s')
I wrote a simple module named colors.py to make this a little more pythonic:
import colors
with colors.pretty_output(colors.BOLD, colors.FG_RED) as out:
out.write("This is a bold red text")
with colors.pretty_output(colors.BG_GREEN) as out:
out.write("This output have a green background but you " +
colors.BOLD + colors.FG_RED + "can" + colors.END + " mix styles")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With