[Tutor] Printing Bold Text

Alan Gauld alan.gauld at yahoo.co.uk
Fri Jan 31 19:38:15 EST 2020


On 31/01/2020 18:30, Adam Slowley wrote:
> So I’m a beginner, learning Python for the first time so this might be a dumb question but here goes.

Not a dumb question, but a surprisingly difficult one.

>  I’m trying to print a text in bold. For example:
> 
> def main():
>     name=input("Enter your name:")
>     print("You entered for name a value of",name)
> 
> I want whatever the user inputs as his/her name to be printed in Bold

The solution to this is rather complex. Python reads and writes to
stdin/out on  the host computer. Python does not know anything about
what stdin/out are other than that it can send text to them or read text
from them. It could be an old style teleprinter, a VDT, a window within
a GUI(the most common these days) or a speech unit for the visually
impaired.

So python has no way of making text bold or otherwise - indeed in the
text reader case it is a meaningless concept, unless it shouted the bold
bits perhaps?

So how is bold text handled? It's down to the device and if that is a
typical text console in a GUI window then it will have a set of control
codes that affect how text is displayed. The three most common sets of
control codes are the VT100(Unix land), the 3270 (IBM mainframes) and
the ANSI terminal (from MS DOS). Once you know which terminal you are
using you can write those codes out. Let's assume you've looked up the
reference material and stored their values in some variables: BOLD and
NORMAL

Then you can do something like:

print(NORMAL, "Enter your name: ", BOLD)
name = input()
print(NORMAL)

That will switch the terminal between BOLD and NORMAL as required.
But its all a bit tedious and the minute your user switches
terminal then it breaks!

[ Note: It is slightly easier to do this in a GUI environment
because you can control the fonts used for the various parts
of the screen. So you could have the label asking for the name
use normal font and the entry box into which you type the reply
use a bold font. but then you have all the extra complexity
of building a GUI!]

As a response to this difficulty the curses library was created
in the 1980's(?) and it has been ported to most environments so
that you can use different font styles and even colours in a
non GUI terminal. It still requires writing out specific text
settings like the BOLD and NORMAL above but at least the codes
are standard and it will work across terminal types without
change. curses is a standard module with Python on MacOS and
Unix but not on Windows. (You need to download a third party
version for Windows, or use the cygwin version of Python).

Most of us just learn to live with not having bold/underline etc when
working in a terminal. It makes life much easier!

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list