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
#1
Question 
Hello everyone.

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

print(spots[1])
print(spots)
why the color only applies to the first print line and the dictionary item does not read the color code?
Output:
ONE #this is colored(yellow) {1: '\x1b[93mONE\x1b[0m', 2: '2', 3: '3'}
I'm using VSCode on windows 10
Reply
#2
x = '\033[93mONE\033[0m'
print(x.__str__(), x.__repr__())
Output:
[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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 903 Oct-25-2023, 09:09 AM
Last Post: codelab
  Color a table cell based on specific text Creepy 11 1,857 Jul-27-2023, 02:48 PM
Last Post: deanhystad
  extract color text from PDF Maha 0 2,036 May-31-2021, 04:05 PM
Last Post: Maha
  Change Text Color Output in Python bluethundr 2 8,677 Mar-06-2019, 10:23 PM
Last Post: bluethundr
  Creating Dictionary form LOG /text file DG1234 7 5,367 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 8,582 Aug-23-2018, 01:16 PM
Last Post: Maia07
  Need help using pathlib to read text file into dictionary gwilli3 4 4,132 Aug-13-2018, 06:21 PM
Last Post: gwilli3
  unable to convert text file in dictionary purnima1 6 4,233 Apr-02-2018, 07:44 AM
Last Post: purnima1
  How to convert Text file contents into a dictionary. tannishpage 12 51,155 May-11-2017, 03:49 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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