0

I need to make a string to bold. I have used:

from colorama import init,Fore, Back, Style

y='Hello world'

I want to pass the bolded 'y' to another variable.

Code Stranger
  • 103
  • 1
  • 10
FRECEENA FRANCIS
  • 181
  • 2
  • 13

2 Answers2

1

I would highly recommend to look into https://github.com/willmcgugan/rich :)

from rich import print

print("Hello, [bold magenta]World[/bold magenta]!")
Moritz Makowski
  • 339
  • 2
  • 16
0

I don't think that there is a way to do that generally. However, using this answer here, you can probably establish your own Bold class best suited to your needs.

import colorama

colorama.init()  # catch the format on windows to print correctly


class Format:
   PURPLE = '\033[95m'
   CYAN = '\033[96m'
   DARKCYAN = '\033[36m'
   BLUE = '\033[94m'
   GREEN = '\033[92m'
   YELLOW = '\033[93m'
   RED = '\033[91m'
   BOLD = '\033[1m'
   UNDERLINE = '\033[4m'
   END = '\033[0m'


class Bold(str):

    def __init__(self, t):
        self.t = t

    def asHTML(self):
        """Returns the text formated for HTML usaage"""
        return f"<b>{self.t}</b>"

    def asMD(self):
        """Returns the text formated for additions to markdown files, like for example a README.md"""
        return "**" + self.t + "**"

    def __str__(self):
        return Format.BOLD + self.t + Format.END


y = 'Hello world'
y = Bold(y)
print(y)
print(y.lower())  # not actually in bold ! 
# Do not forget to overwrite any function like this to 'reboldify' the text

which for me prints :

Hello world

hello world

Alex SHP
  • 133
  • 9