Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
f-strings and termcolor
#1
Hi!

This problem came up when I was searching for ways to colour and give styles to strings:
https://python-forum.io/Thread-colouring-strings?page=2

The f-string formatting seems to be the advisable choice since Python 3.6, so I want to be able to learn properly about them.
I want to compare different packages applying the same formatting (f-strings) to the same string with all the packages, to be fair in the comparison.

With all the other packages in the comparison group, I managed finally to make them work, but not with termcolor. Due to the way the attributes are applied, I cannot make it work with f-strings. It works if I don't use f-strings, but for the comparison, I'd really like to use them. Please, is there a way to do it?

This is the little program I use with the string to be formatted (I commented out the two lines that I tried with f-strings). The program works, but I wanted to apply f-strings to the coding. What am I doing wrong?:
# strings_compared_EXTRA_01.py
#

import sys
from termcolor import colored, cprint

# str1 = "dangerous", "RED", attrs=["bold", "underline"]
# cprint(f'\nThis is  {str1} ... for my mental health!\n\n')

cprint('\nThis is ', end='')
cprint('dangerous', 'green', attrs=['bold', 'underline'], end='')
cprint('... for my mental health!', end='\n\n')
It works, but I wanted to make it work with f-strings:
[Image: dangerous.png]

Could you help me, please?

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Messages In This Thread
f-strings and termcolor - by newbieAuggie2019 - Sep-21-2019, 03:37 PM
RE: f-strings and termcolor - by ndc85430 - Sep-21-2019, 03:42 PM
RE: f-strings and termcolor - by newbieAuggie2019 - Sep-21-2019, 04:34 PM
RE: f-strings and termcolor - by ThomasL - Sep-21-2019, 05:08 PM
RE: f-strings and termcolor - by newbieAuggie2019 - Sep-22-2019, 03:52 AM
RE: f-strings and termcolor - by snippsat - Sep-21-2019, 07:32 PM
RE: f-strings and termcolor - by santarosajoe - Jul-28-2020, 02:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 822 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,824 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Finding multiple strings between the two same strings Slither 1 2,556 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  colorama/termcolor not returning coloured lines kapilan15 3 6,899 Jan-14-2019, 12:57 PM
Last Post: buran
  lists, strings, and byte strings Skaperen 2 4,270 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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