Python Forum
How to print the current time in color
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print the current time in color
#1
Hi,

I want to create a variable, and when I print that variable, it needs to be in a certain color. This is my current code

import datetime
from colored import fg, attr


current_time = datetime.datetime.now().strftime('%H:%M:%S') % (fg(151), attr(0))
print(current_time)
Error:
Traceback (most recent call last): File "C:/Users/Julius/PycharmProjects/Fenix/venv/Grosbasket script.py", line 8, in <module> current_time = datetime.datetime.now().strftime('%H:%M:%S') % (fg(151), attr(0)) TypeError: not all arguments converted during string formatting
How do I solve this error?
Reply
#2
This works for me on python3.8
import timedate
from colored import fg
current_time = datetime.datetime.now().strftime('%H:%M:%S')
print(fg(11),current_time)
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
You're just using the bare string created by strftime(). You created a format string (with the %) and gave it the attributes, but it couldn't find a "%s" or similar to insert the data.

You just need concatenate those attributes, either in the print itself, or create a new string with them inside and print the new string.

One method
data_to_print = "Hello there" #could be your datetime
print(fg(151), data_to_print, attr(0))
Reply
#4
(Apr-15-2020, 09:13 PM)bowlofred Wrote: You're just using the bare string created by strftime(). You created a format string (with the %) and gave it the attributes, but it couldn't find a "%s" or similar to insert the data.

You just need concatenate those attributes, either in the print itself, or create a new string with them inside and print the new string.

One method
data_to_print = "Hello there" #could be your datetime
print(fg(151), data_to_print, attr(0))
cool thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 902 Oct-25-2023, 09:09 AM
Last Post: codelab
  Print names in x-axis of a time-series values hobbyist 4 1,178 Apr-22-2023, 09:29 PM
Last Post: deanhystad
  How to print results of asyncio websockets at the same time? codingmonster 0 1,744 Jun-04-2021, 01:48 PM
Last Post: codingmonster
  Print characters in a single line rather than one at a time hhydration 1 1,990 Oct-10-2020, 10:00 PM
Last Post: bowlofred
  How to print n days back date at give time Mekala 1 1,956 Oct-10-2020, 03:35 AM
Last Post: bowlofred
  Having a hard time conceptualizing how to print something MysticLord 6 3,041 Sep-19-2020, 10:43 PM
Last Post: MysticLord
  Print a certain string only the first time it appears in a test file buttercup 5 2,698 Jul-23-2020, 01:30 PM
Last Post: palladium
  calling strftime does not display current time tonytech 2 2,084 May-05-2020, 04:53 AM
Last Post: tonytech
  Print date, Time and output to file tpolim008 3 2,282 Mar-26-2020, 06:49 PM
Last Post: ndc85430
  Make color for string when print to console hadoan 6 3,342 Oct-31-2019, 01:40 PM
Last Post: hadoan

Forum Jump:

User Panel Messages

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