Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1061885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:32:26+00:00 2026-05-16T18:32:26+00:00

Is there a way to print string in colorful way with python? For example,

  • 0

Is there a way to print string in colorful way with python?

For example, can I print some part of string red or something to a console? I use Mac OS X.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:32:27+00:00Added an answer on May 16, 2026 at 6:32 pm

    This works for linux consoles that support color:

    CODE={
        'ENDC':0,  # RESET COLOR
        'BOLD':1,
        'UNDERLINE':4,
        'BLINK':5,
        'INVERT':7,
        'CONCEALD':8,
        'STRIKE':9,
        'GREY30':90,
        'GREY40':2,
        'GREY65':37,
        'GREY70':97,
        'GREY20_BG':40,
        'GREY33_BG':100,
        'GREY80_BG':47,
        'GREY93_BG':107,
        'DARK_RED':31,
        'RED':91,
        'RED_BG':41,
        'LIGHT_RED_BG':101,
        'DARK_YELLOW':33,
        'YELLOW':93,
        'YELLOW_BG':43,
        'LIGHT_YELLOW_BG':103,
        'DARK_BLUE':34,
        'BLUE':94,
        'BLUE_BG':44,
        'LIGHT_BLUE_BG':104,
        'DARK_MAGENTA':35,
        'PURPLE':95,
        'MAGENTA_BG':45,
        'LIGHT_PURPLE_BG':105,
        'DARK_CYAN':36,
        'AUQA':96,
        'CYAN_BG':46,
        'LIGHT_AUQA_BG':106,
        'DARK_GREEN':32,
        'GREEN':92,
        'GREEN_BG':42,
        'LIGHT_GREEN_BG':102,
        'BLACK':30,
    }
    
    def termcode(num):
        return '\033[%sm'%num
    
    def colorstr(astr,color):
        return termcode(CODE[color])+astr+termcode(CODE['ENDC'])
    
    if __name__=='__main__':
        astr='yippy skippy'
        # for num in range(300):
        #     color=termcode(num)
        #     print('%s: %s'%(num,color+astr+termcode(CODE['ENDC'])))
        for key in sorted(CODE.keys()):
            print('%s: %s'%(key,colorstr(astr,key)))
    
        print(colorstr('Red','RED'))
    

    Here is an example of using colorstr to make a colorized logger:

    import logging
    import copy
    
    logger=logging.getLogger(__name__)
    
    class ColoredFormatter(logging.Formatter):
        # A variant of code found at http://stackoverflow.com/questions/384076/how-can-i-make-the-python-logging-output-to-be-colored
        LEVELCOLOR = {
            'DEBUG': 'BLUE',
            'INFO': 'BLACK',
            'WARNING': 'PURPLE',
            'ERROR': 'RED',
            'CRITICAL': 'RED_BG',
            }
    
        def __init__(self, msg):
            logging.Formatter.__init__(self, msg)
    
        def format(self, record):
            record = copy.copy(record)
            levelname = record.levelname
            if levelname in self.LEVELCOLOR:
                record.levelname = colorstr(levelname,self.LEVELCOLOR[levelname])
                record.name = colorstr(record.name,'BOLD')
                record.msg = colorstr(record.msg,self.LEVELCOLOR[levelname])
            return logging.Formatter.format(self, record)
    
    if __name__=='__main__':
        logger.setLevel(logging.DEBUG)
        console = logging.StreamHandler()
        console.setFormatter(
            ColoredFormatter('%(name)s: %(message)s (%(filename)s:%(lineno)d)'))
        logger.addHandler(console)
        fh = logging.FileHandler('/tmp/test.log','w')
        fh.setFormatter(logging.Formatter('%(name)s: %(message)s'))
        logger.addHandler(fh)
    
        logger.debug('debug')
        logger.info('info')
        logger.warning('Warning')
        logger.error('ERROR')
        logger.critical('CRITICAL!!!')
    

    This example logs to both the console (stderr) and a file (/tmp/test.log). The message to the console is colorized, but the output to the file is left plain.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.