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

Due to time constraints, I'm working with a lot of issues at the same time: f-strings, functions, loops... and I was wondering... with so many formatting ways, is there a straightforward way to format the text of a string with characteristics like font type, bold, italics, underlined, size, colour, etc. in a similar way with tags, like in here?:
print("\n\nThis is a [b]test[/b] \
to [color=red]check[/color] and [size=8]other[/size]\
[font=Tahoma]formatting[/font]types.")
That little program produces the following failed formatted string test:
Output:
This is a [b]test[/b] to [color=red]check[/color] [size=8]other[/size][font=Tahoma]formatting[/font]types.
All the best,
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
#2
https://pypi.org/project/colorama/ ?
https://python-prompt-toolkit.readthedoc..._text.html ?

I'm not sure how to deal with the text size
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(Sep-17-2019, 03:14 AM)wavic Wrote: https://pypi.org/project/colorama/ ?
https://python-prompt-toolkit.readthedoc..._text.html ?

I'm not sure how to deal with the text size

Thank you very much indeed!

I had seen about colorama, searching for colouring strings, but I didn't know about termcolor.

I was a bit worried about these kinds of packages, because I'm a newbie, and you need to import them and make other modifications inside your programs to make them work... and I was right... I don't know what I'm doing wrong, but I cannot make it work in my Windows 10. I'm using Python 3.7.4 and somewhere I read that it's with batteries included... Well, I beg to differ... I need colour in my world, and I think it should be included within the original Python download package, but oh, blimey, I cannot do something about it. Undecided

I'm going to show what I have done, and maybe somebody is kind enough to indicate me what I'm doing wrong.

Okay, first, I gave colorama a try, so I used pip installation, which seems to have been successful:
[Image: successful-colorama-installation.png]
Then I tried a test program:
import colorama
from colorama import init
init()
from colorama import Fore, Back, Style

print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')

print('\033[31m' + 'some red text')
print('\033[30m') # and reset to default color
with the following failed output:
Output:
<-[31msome red text <-[42mand with a green background <-[2mand in dim text <-[0m back to normal now <-[31msome red text <-[30m
Then I tried with a different test program from a different site:
import colorama

# If using Windows, init() will cause anything sent to stdout or stderr
# will have ANSI color codes converted to the Windows versions. Hooray!
# If you are already using an ANSI compliant shell, it won't do anything
colorama.init()

# Now regular ANSI codes should work, even in Windows
CLEAR_SCREEN = '\033[2J'
RED = '\033[31m'   # mode 31 = red forground
RESET = '\033[0m'  # mode 0  = reset
print(CLEAR_SCREEN + RED + 'Welcome!' + RESET)
producing an equally failed output:
Output:
<-[2J<-[31mWelcome!<-[0m
I tried with a different test program from another site:
import colorama
from colorama import Fore, Back, Style
colorama.init()

# Set the color semi-permanently
print(Fore.CYAN)
print("Text will continue to be cyan")
print("until it is reset or changed")
print(Style.RESET_ALL)

# Colorize a single line and then reset
print(Fore.RED + 'You can colorize a single line.' + Style.RESET_ALL)

# Colorize a single word in the output
print('Or a single ' + Back.GREEN + 'words' + Style.RESET_ALL + ' can be highlighted')

# Combine foreground and background color
print(Fore.BLUE + Back.WHITE)
print('Foreground, background, and styles can be combined')
print("==========            ")

print(Style.RESET_ALL)
print('If unsure, reset everything back to normal.')
and another fail:
Output:
<-[36m Text will continue to be cyan until it is reset or changed <-[0m <-[31mYou can colorize a single line.<-[0m Or a single <-[42mwords<-[0m can be highlighted <-[34m<-[47m Foreground, background, and styles can be combined ========== <-[0m If unsure, reset everything back to normal.
So I decided then to give termcolor a try, and also it seems that it was a successful installation:
[Image: successful-termcolor-installation.png]
Then I used the following test program from the termcolor site:
import sys
from termcolor import colored, cprint

text = colored('Hello, World!', 'red', attrs=['reverse', 'blink'])
print(text)
cprint('Hello, World!', 'green', 'on_red')

print_red_on_cyan = lambda x: cprint(x, 'red', 'on_cyan')
print_red_on_cyan('Hello, World!')
print_red_on_cyan('Hello, Universe!')

for i in range(10):
    cprint(i, 'magenta', end=' ')

cprint("Attention!", 'red', attrs=['bold'], file=sys.stderr)
with the following once more failed attempt:
Output:
<-[5m<-[7m<-[31mHello, World!<-[0m <-[41m<-[32mHello, World!<-[0m <-[46m<-[31mHello, World!<-[0m <-[46m<-[31mHello, Universe!<-[0m <-[35m0<-[0m <-[35m1<-[0m <-[35m2<-[0m <-[35m3<-[0m <-[35m4<-[0m <-[35m5<-[0m <-[35m6<-[0m <-[35m7<-[0m <-[35m8<-[0m <-[35m9<-[0m <-[1m<-[31mAttention!<-[0m
Thanks in advance, any help would be appreciated.

All the best,

I just noticed that indeed the last part of the output shown for termcolor was indeed printed in red, but just the last part. And as it says Attention!, probably my mind played a trick on me. It was shown in red, not in bold type as it should, but at least it could help to find an answer. This was printed in red (like an error message) at the end of the output (the rest of the output was shown in the regular blue coloured font type on a white background colour, in my Python 3.7.4 Shell running on Windows 10:
<-[1m<-[31mAttention!<-[0m
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
#4
Need a better Terminal shell than cmd/Poweshell.
I only use cmder | Github
Also remember to do .\cmder.exe /REGISTER ALL.
So get a shortcut in file explorer,to go directly to cmder.
Test term.py is your latest code.
[Image: hFxVCt.png]
Reply
#5
As it seems that only the last part was working I modified the program as:
import sys
from termcolor import colored, cprint


cprint("Attention!", 'red', attrs=['bold'], file=sys.stderr)
cprint("Attention!", 'green', attrs=['bold'], file=sys.stderr)
cprint("Attention!", 'yellow', attrs=['bold'], file=sys.stderr)
cprint("Attention!", 'cyan', attrs=['bold'], file=sys.stderr)
cprint("Attention!", 'grey', attrs=['bold'], file=sys.stderr)
cprint("Attention!", 'blue', attrs=['bold'], file=sys.stderr)
cprint("Attention!", 'white', attrs=['bold'], file=sys.stderr)
cprint("Attention!", 'magenta', attrs=['bold'], file=sys.stderr)
but then everything was printed in red, also with the annoying arrows, brackets and ANSI code numbers:
<-[1m<-[31mAttention!<-[0m
<-[1m<-[32mAttention!<-[0m
<-[1m<-[33mAttention!<-[0m
<-[1m<-[36mAttention!<-[0m
<-[1m<-[30mAttention!<-[0m
<-[1m<-[34mAttention!<-[0m
<-[1m<-[37mAttention!<-[0m
<-[1m<-[35mAttention!<-[0m


(Sep-17-2019, 04:42 PM)snippsat Wrote: Need a better Terminal shell than cmd/Poweshell.
I only use cmder | Github
Also remember to do .\cmder.exe /REGISTER ALL.
So get a shortcut in file explorer,to go directly to cmder.
Test term.py is your latest code.
[Image: hFxVCt.png]
Thanks a lot!
It looks promising, but when executing Cmder.exe, I received a message from my Avast antivirus saying:
Blocked threat
We have blocked init.bat because it was infected with IDP.ALEXA.51


What should I do???
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
#6
(Sep-17-2019, 04:48 PM)newbieAuggie2019 Wrote: What should I do???
Put cmder folder in Global Whitelist.
Reply
#7
(Sep-17-2019, 05:13 PM)snippsat Wrote:
(Sep-17-2019, 04:48 PM)newbieAuggie2019 Wrote: What should I do???
Put cmder folder in Global Whitelist.
Thanks again!

I just used the link you provided and there, it says to be sure it doesn't contain any viruses by checking in the free VirusTotal site, and although almost
Quote:No engines detected this file
two of them were
Quote:Unable to process file type
, and most importantly, there was the following message on the community section:

Output:
Signature Match - THOR APT Scanner Detection ============================ Rule: Cmder_alt_commandline Rule Set: Hacktools 1 Rule Type: - Description: - Reference: - Author: - Score: - Detection Snapshot ============================ Detection Timestamp: 2019-08-19 00:44 AV Detection Ratio: 2 / 71 LOW AV DETECTION #cmder #alt #Hacktools1 #Cmder_alt_commandline More information: https://www.nextron-systems.com/notes-on-virustotal-matches/ Please report interesting findings via Twitter @thor_scanner

It seems that I'm not alone having that message:
GitHub - IDP.ALEXA.51 threat
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
#8
It's a just false positive,the two engine that defect something is mediocre/bad Chinese AV's.
Reply
#9
Windows is a bit tricky about the text formatting. Try prompt_toolkit. It's more mature.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
(Sep-17-2019, 04:42 PM)snippsat Wrote: I only use cmder | Github
Thanks again, I'm trying hard, but it seems I'm useless. Wall
(Sep-17-2019, 04:42 PM)snippsat Wrote: Also remember to do .\cmder.exe /REGISTER ALL.
So get a shortcut in file explorer,to go directly to cmder.
Test term.py is your latest code.
How do you that?
(Sep-17-2019, 04:42 PM)snippsat Wrote: [Image: hFxVCt.png]
That image looks really nice, but how do you get it? I didn't see any information about it.
Finally, I managed to install it, but I think I did it wrong, because I tried your suggestion (term.py) and others from the cmder site, and I'm not sure I have done it right:
Output:
D:\new downloads 28102017\cmder λ term.py 'term.py' is not recognized as an internal or external command, operable program or batch file. D:\new downloads 28102017\cmder λ ls bin/ Cmder.exe* config/ icons/ LICENSE vendor/ 'Version 1.3.12.915' D:\new downloads 28102017\cmder λ icons/ 'icons' is not recognized as an internal or external command, operable program or batch file. D:\new downloads 28102017\cmder λ gl fatal: not a git repository (or any of the parent directories): .git D:\new downloads 28102017\cmder λ where cmder.exe D:\new downloads 28102017\cmder\Cmder.exe
Maybe it's something related to paths. I always have problems with paths (I had them with Python when I tried to install it in a different folder than the root directory)... Doh

All the best,

(Sep-17-2019, 03:14 AM)wavic Wrote: https://pypi.org/project/colorama/ ?
https://python-prompt-toolkit.readthedoc..._text.html ?
Huh Did you change the links?... I would have sworn they were about colorama and termcolor... Confused Maybe I'm becoming crazy... Big Grin
(Sep-17-2019, 03:14 AM)wavic Wrote: https://python-prompt-toolkit.readthedoc..._text.html ?
Thank you again. I'll give it a try too, as I'm becoming rather frustrated at my incompetence to make the previous ones work... Cry
I just want to put a bit of colour in my life Dance , coding in a nice environment and producing a cool output... Big Grin

All the best,
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 697 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,702 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Finding multiple strings between the two same strings Slither 1 2,479 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  lists, strings, and byte strings Skaperen 2 4,181 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