Python Forum
Text color (ascii) in a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Text color (ascii) in a dictionary
Hello everyone.

In this code:
spots = {1: '1', 2: '2', 3: '3'}
spots[1] = '\033[93mONE\033[0m'

why the color only applies to the first print line and the dictionary item does not read the color code?
ONE #this is colored(yellow) {1: '\x1b[93mONE\x1b[0m', 2: '2', 3: '3'}
I'm using VSCode on windows 10
x = '\033[93mONE\033[0m'
print(x.__str__(), x.__repr__())
[color=#F1C40F]ONE[/color] '\x1b[93mONE\x1b[0m'
You are seeing the difference of how print() prints a unicode string using __str__() and __repr__(). __str__() is a "pretty" way to print that is meant for output. __repr__() is more for debugging. If your program crashes and the error trace displays values, you want to see them in a more raw form, and that is what __repr__() is for. The dict class author decide that when you print a dictionary you want to see the more raw values.
banidjamali likes this post

Possibly Related Threads…
Thread Author Replies Views Last Post
  ascii question DPaul 9 844 Aug-21-2021, 05:58 PM
Last Post: Yoriz
  extract color text from PDF Maha 0 538 May-31-2021, 04:05 PM
Last Post: Maha
  Change Text Color Output in Python bluethundr 2 6,991 Mar-06-2019, 10:23 PM
Last Post: bluethundr
  Creating Dictionary form LOG /text file DG1234 7 2,406 Feb-13-2019, 08:08 PM
Last Post: DG1234
  Is there a way to detect the text font, size and color from an image in python? Maia07 2 5,271 Aug-23-2018, 01:16 PM
Last Post: Maia07
  Need help using pathlib to read text file into dictionary gwilli3 4 2,310 Aug-13-2018, 06:21 PM
Last Post: gwilli3
  unable to convert text file in dictionary purnima1 6 2,435 Apr-02-2018, 07:44 AM
Last Post: purnima1
  ascii() (repr() in py2) Skaperen 1 2,751 Jun-05-2017, 10:32 PM
Last Post: Ofnuts
  How to convert Text file contents into a dictionary. tannishpage 12 45,336 May-11-2017, 03:49 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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