Python: color output with multiple variables

Francesco Mantovani

I'm trying to print Python output on the Terminal.

Everything works fine if I have to print a phrase, problem starts when the phrase contains variables.

This is the code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import datetime
from colorama import init
init(autoreset=True)
from fabric.colors import red, green

latitude =  36.02866
longitude = 70.77258

print ("\033[92m" + 'I know I can be green' + "\033[0m")
print ("\033[92m" +'I want the whole line to be green or red',latitude,',',longitude, 'at', datetime.datetime.now(), "\033[0m")
print ("\033[91m" + 'I know I can be red' + "\033[0m")
print ('automatically back to default color again')

As you can see just the first part of the second phrase is colored.

enter image description here

But I need the whole phrase to be colored!

PRMoureu

This should work by formating the string, instead of giving new parameters to the function print() :

print ("\033[92m" +'I want the whole line to be green or red {0}, {1} at {2}'.format(
    latitude, longitude, datetime.datetime.now())+ "\033[0m")
print ("\033[91m" + 'I know I can be red' + "\033[0m")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Optimizing multiple output variables of a function in Python

Python pandas generate a table for multiple output variables

Change Color Output in Python in windows

loop multiple variables in python

Reading in Multiple Variables Python

Python "in" with multiple variables

Format multiple python variables

Handling multiple variables in python

How to store curl output in multiple variables in Bash

Return multiple output variables from postgres function

Optimize code for output multiple int variables into String

In ash how to set multiple line output to variables

R basics: working with multiple variables at once and their output

Reading delimited output into multiple Bash variables

powershell output multiple variables derived from processlist

Set multiple variables to different fields of awk output

SQL Output Multiple Local Variables into one Column

using jq to assign multiple output variables

Using empty() to adjust output with multiple variables

How to output multiple variables in one line

Divide an output into multiple variables using shell script

How to output multiple variables using Azure Kusto?

How to store multiple lines of output in separate variables?

How to parse multiple line output as separate variables

'Source' output of a command in bash script as multiple variables

Multiple color argument in plotly in python

I can not properly output the color text in python

How do I use multiple loops and store the output to multiple variables?

Creating dummy variables for multiple cateogorical variables in Python