Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
colouring strings
#16
Hi again!

I’ve been trying to find an easy and straightforward way to manipulate strings, giving them colour, and/or applying characteristics like different types of fonts, size, with bold, underline, italic and blinking attributes, among others.

I have spent a lot of time looking for possibilities and choices for Python 3, then downloading them, studying their documentation, making little programs to verify their claims, and finally getting frustrated for not meeting my expectations or even in some cases their own claims.

If you had wished also to work with colours and styles, maybe the information I’m going to share here might help you to choose one or another of the packages. Nevertheless, please keep in mind that it is my personal opinion and that it can be different from yours.

I’m going to compare 5 of these options, and show you the output with these options in my computer running on Windows 10. Why do I explicitly say that my computer runs on Windows 10?

Well, because it seems that that is the reason why these programs don’t behave like they should. I’ve seen images and read comments from users with computers running on Linux, Ubuntu and the like, that show these programs behaving like they should.

I wonder why it seems that Microsoft doesn’t seem to provide a solution for this problem that it appears to be related to how ANSI escape character sequences (for producing coloured terminal text and cursor positioning) are handled by the computer. These ANSI escape characters don't work in Windows. They will work on ANSI consoles like Linux shells, but not in Windows. Windows has their own escape characters. It makes you think that there is no wonder that many users change the Microsoft owned Windows operating system (OS) of their computers, for others like Linux or Ubuntu (even if it might be not only for this). As I have read in some places, this issue is not new and it has been around for more than five years. You could say that in that time and with all the millions of dollars that Microsoft has earned through these years, they could have put a team to sort this out. Well, if they have … it doesn’t say much about their competence if in five years , we keep on having the same problem not solved yet …

I’m going to provide here some data and comments about five packages, that I have been testing and working with Python 3.7.4 for a colourful output, on a bit deeper level than others that I’ll mention later on. First, the essentials:

colorama 0.4.1
https://pypi.org/project/colorama/
information on site: Cross-platform coloured terminal text.
Last release: ‎Nov‎ ‎25‎, ‎2018.

Termcolor 1.1.0
https://pypi.org/project/termcolor/
information on site: ANSII Color formatting for output in terminal.
Last release: ‎Jan‎ ‎13‎, ‎2011.

prompt_toolkit 2.0.9
https://pypi.org/project/prompt_toolkit/
information on site: Library for building powerful interactive command lines in Python.
Last release: ‎Feb‎ ‎19‎, ‎2019.

sty 1.0.0b12
https://pypi.org/project/sty/
information on site: Simple, flexible and extensible string styling for your terminal.
Last release: ‎Sep‎ ‎10‎, ‎2019.

colorful 0.5.4
https://pypi.org/project/colorful/
information on site: Terminal string styling done right, in Python.
Last release: ‎Sep‎ ‎21‎, ‎2019.

And now some comments on them:

colorama 0.4.1
https://pypi.org/project/colorama/
information on site: Cross-platform coloured terminal text.
Last release: ‎Nov‎ ‎25‎, ‎2018.

It seems to be one of the most popular packages around.

According to the site, it makes ANSI escape character sequences (for producing coloured terminal text and cursor positioning) work under MS Windows. ANSI escape character sequences have long been used to produce coloured terminal text and cursor positioning on Unix and Macs. Colorama makes this work on Windows, too.

I thought this was the solution to my problems, as it seemed that there was a lot of information about it and everyone was mentioning colorama when speaking of a way to give colour and attributes to strings. Unfortunately, I was saddened when I read an answer from Jonathan Hartley (Tartley), the author of the project, to a user asking if colorama had italic style:

“It is my opinion that Colorama shouldn't try to (badly) duplicate all the functionality that is already available very thoroughly and elegantly in packages like termcolor or the amazing blessings. To be honest, I regret including all of the colours and styles that are already built into colorama [ … ] I wish we could just do one thing (Windows compatibility) and do it well.”

and even more disheartening when he followed up saying:

“I consider myself retired from the project.”

I think it’s a pity, although he left other contributors in charge of the project.
https://github.com/tartley/colorama/issues/122

Termcolor 1.1.0
https://pypi.org/project/termcolor/
information on site: ANSII Color formatting for output in terminal.
Last release: ‎Jan‎ ‎13‎, ‎2011.

It seems to be also, one of the most popular packages around.
It is even praised by Jonathan Hartley (Tartley), the author of colorama. (The last release of termcolor was almost eight years prior to the last release of colorama).
I think it has similar capabilities to the other 4 packages in the group of 5 that I’m testing.

prompt_toolkit 2.0.9
https://pypi.org/project/prompt_toolkit/
information on site: Library for building powerful interactive command lines in Python.
Last release: ‎Feb‎ ‎19‎, ‎2019.

On the description, there were some parts that caught my eye: Runs on all Python versions from 2.6 up to 3.7. Works well with Unicode double width characters (Chinese input). Runs on Linux, OS X, FreeBSD, OpenBSD and Windows systems. The last release was 7 months ago.

I also like the fact that it can be used with HTML-like tags. So we can use instructions like these:
print_formatted_text(HTML('<b>This is bold</b>')) 
print_formatted_text(HTML('<i>This is italic</i>')) 
print_formatted_text(HTML('<u>This is underlined</u>'))
sty 1.0.0b12
https://pypi.org/project/sty/
information on site: Simple, flexible and extensible string styling for your terminal.
Last release: ‎Sep‎ ‎10‎, ‎2019.

One of my last candidates that I had set my hopes on, as its last release was just twelve days ago, so I thought that some problems would have been fixed by now, even more when on the description, they said that recent versions of Windows 10 should work with this package as well.

colorful 0.5.4
https://pypi.org/project/colorful/
information on site: Terminal string styling done right, in Python.
Last release: ‎Sep‎ ‎21‎, ‎2019.

This is just my last candidate (the original comparison was with only the four previous ones, but as I keep going on in my search for the Holy Grail, I saw this one, whose last release was YESTERDAY!!!).

Well, now I’ll show a small program adapted to each of these 5 packages to print a formatted string, using the attributes for bold, underline, italic and colour and then the output image, using Cmder as the output terminal, because if I use the Python 3.7.4 Shell, we’d only see a bunch of numbers, slashes and square brackets, beside a not colourful nor formatted text.

First, for colorama (I’ve tried to apply the f-string formatting on all the five packages):

# strings_compared_01_BIS_FINAL.py
#

import colorama
from colorama import init
init()
from colorama import Fore, Back, Style

print(f'\nI like {Style.BRIGHT}{Fore.GREEN}python-forum{Style.RESET_ALL} ... a lot!')

# MY COMMENTS:
#
# 1) The author of colorama called properly 'BRIGHT' to what others called
# 'bold'; that is to say, it shows as a bit bright, but not 'bold'. 
#  
# 2) Colorama only has the following Styles (in other programs called
# attributes): DIM, NORMAL, BRIGHT, RESET_ALL. Actually, NORMAL and RESET_ALL
# do not do the same, because while NORMAL would change everything but the
# colour, RESET_ALL will put back to normal, all the previous settings,
# including the colour. DIM and BRIGHT are hardly perceptible. 
#
# 3) COLORAMA HAS NO 'ITALIC' or 'UNDERLINE' attributes (Styles in colorama).
#
# 4) For what I've found, it seems that the reason for 'bold', 'italic'
# and other characteristics not working, is due to the computer running
# on a Microsoft Windows Operating System (OS), as it seems that when
# it's running on Ubuntu or similar, there is not such a problem!!!
Second, for termcolor (I’ve tried to apply the f-string formatting on all the five packages, but I couldn’t manage it with termcolor):

# strings_compared_02_BIS_FINAL.py
#

import sys
from termcolor import colored, cprint

cprint('\nI like ', end='')
cprint('python-forum', 'green', attrs=['bold', 'underline'], end='')
cprint(' ... a lot!', end='\n')

# MY COMMENTS:
#
# 1) The attribute 'bold' doesn't seem to work. Actually, it seems 
# to be maybe a bit brighter, but not 'bold'. 
#  
# 2) I had to eliminate  attrs=['italic'], because 'termcolor' has 
# no attribute for 'italic'.
#
# 3) The 'underline' works. HOORAY!
#
# 4) For what I've found, it seems that the reason for 'bold', 'italic'
# and other characteristics not working, is due to the computer running
# on a Microsoft Windows Operating System (OS), as it seems that when
# it's running on Ubuntu or similar, there is not such a problem!!!
Third, for prompt_toolkit:

# strings_compared_03_BIS_FINAL.py
#

from __future__ import unicode_literals, print_function
from prompt_toolkit import print_formatted_text, HTML

print_formatted_text(HTML(f'\nI like <ansigreen><b><u>python-forum</u></b></ansigreen><i> ... a lot!</i>'))

# MY COMMENTS:
#
# 1) The attribute 'bold' doesn't seem to work. Actually, it seems 
# to be maybe a bit brighter, but not 'bold'. 
#  
# 2) Although 'prompt_toolkit' has the attribute 'italic', it doesn't
# seem to work here.
#
# 3) The 'underline' works. HOORAY!
#
# 4) For what I've found, it seems that the reason for 'bold', 'italic'
# and other characteristics not working, is due to the computer running
# on a Microsoft Windows Operating System (OS), as it seems that when
# it's running on Ubuntu or similar, there is not such a problem!!!
Fourth, for sty (I’ve tried to apply the f-string formatting on all the five packages):

# strings_compared_04_BIS_FINAL.py
#

from sty import ef, fg, rs

print(f'\n{"I like "}{ef.b + ef.u + fg.li_green + "python-forum" + rs.fg + rs.u + rs.bold_dim}{ef.i + " … a lot!" + rs.i}')

# MY COMMENTS:
#
# 1) The attribute 'bold' doesn't seem to work. Actually, it seems 
# to be maybe a bit brighter, but not 'bold'. 
#  
# 2) The attribute for 'italic' doesn't work here.
#
# 3) The 'underline' works. HOORAY!
#
# 4) For what I've found, it seems that the reason for 'bold', 'italic'
# and other characteristics not working, is due to the computer running
# on a Microsoft Windows Operating System (OS), as it seems that when
# it's running on Ubuntu or similar, there is not such a problem!!!
And finally on this comparison, fifth for colorful (I’ve tried to apply the f-string formatting on all the five packages, but in this case, I find the formatting very similar to f-strings, so I left the 'f' out, keeping the format of colorful):

# strings_compared_05_BIS_FINAL.py
#

import colorful as cf

cf.print('\nI like {c.bold_underlined_green}python-forum{c.reset}{c.italic} ... a lot!{c.reset}')

# MY COMMENTS:
#
# 1) The attribute 'bold' doesn't seem to work. Actually, it seems 
# to be maybe a bit brighter, but not 'bold'. 
#  
# 2) The attribute for 'italic' doesn't work here.
#
# 3) The 'underline' works. HOORAY!
#
# 4) For what I've found, it seems that the reason for 'bold', 'italic'
# and other characteristics not working, is due to the computer running
# on a Microsoft Windows Operating System (OS), as it seems that when
# it's running on Ubuntu or similar, there is not such a problem!!!
#
# I find the format very similar to f-strings formatting (I didn't
# use the f', though).
Here are the image capture of the 5 packages used to print the same string.
[Image: strings-compared-5-packages.png]

where you can see that the output for the five packages is very similar, with the exception of colorama, that hasn’t the style of 'underline'.
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Messages In This Thread
colouring strings - by newbieAuggie2019 - Sep-17-2019, 03:06 AM
RE: colouring strings - by wavic - Sep-17-2019, 03:14 AM
RE: colouring strings - by newbieAuggie2019 - Sep-17-2019, 04:23 PM
RE: colouring strings - by snippsat - Sep-17-2019, 04:42 PM
RE: colouring strings - by newbieAuggie2019 - Sep-17-2019, 09:46 PM
RE: colouring strings - by newbieAuggie2019 - Sep-17-2019, 04:48 PM
RE: colouring strings - by snippsat - Sep-17-2019, 05:13 PM
RE: colouring strings - by newbieAuggie2019 - Sep-17-2019, 05:37 PM
RE: colouring strings - by snippsat - Sep-17-2019, 06:22 PM
RE: colouring strings - by wavic - Sep-17-2019, 08:50 PM
RE: colouring strings - by snippsat - Sep-17-2019, 10:11 PM
RE: colouring strings - by newbieAuggie2019 - Sep-20-2019, 07:27 AM
RE: colouring strings - by wavic - Sep-18-2019, 08:25 AM
RE: colouring strings - by wavic - Sep-18-2019, 11:10 AM
RE: colouring strings - by newbieAuggie2019 - Sep-20-2019, 06:34 PM
RE: colouring strings - by newbieAuggie2019 - Sep-22-2019, 07:12 AM
RE: colouring strings - by newbieAuggie2019 - Sep-22-2019, 09:07 AM
RE: colouring strings - by jquast - Oct-22-2019, 02:28 PM
RE: colouring strings - by newbieAuggie2019 - Oct-22-2019, 03:31 PM
RE: colouring strings - by newbieAuggie2019 - Sep-24-2019, 10:42 AM
RE: colouring strings - by wavic - Sep-24-2019, 01:28 PM
RE: colouring strings - by newbieAuggie2019 - Sep-30-2019, 08:20 PM
RE: colouring strings - by jquast - Oct-23-2019, 03:44 AM
RE: colouring strings - by newbieAuggie2019 - Oct-23-2019, 08:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 816 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,818 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Finding multiple strings between the two same strings Slither 1 2,547 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  lists, strings, and byte strings Skaperen 2 4,264 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020