10
print("hello")

The output should be the word "hello", but underlined.

Ricky Su
  • 295
  • 1
  • 7
  • 10
  • 1
    Possible duplicate of [How do I print bold text in Python?](http://stackoverflow.com/questions/8924173/how-do-i-print-bold-text-in-python) – Timothée Poisot Feb 15 '16 at 03:27

7 Answers7

31

You can do it by using escape characters.

print("\033[4mhello\033[0m")
dspencer
  • 4,297
  • 4
  • 22
  • 43
Akira Okumura
  • 1,816
  • 2
  • 20
  • 41
  • 2
    It's not working for me, it just print - [4mhello[0m – AlokThakur Feb 15 '16 at 07:27
  • @AlokThakur according to [this comment from a similar question](https://stackoverflow.com/questions/55065640/python-string-formatter-to-get-underlined-headline#comment96879594_55065815), it may not work on terminals that don't understand ANSI escape sequences. – syockit Jun 05 '20 at 00:37
  • See this answer to make it work in windows: https://stackoverflow.com/questions/12492810/python-how-can-i-make-the-ansi-escape-codes-to-work-also-in-windows/64222858#64222858 – Gary Vernon Grubb Oct 27 '20 at 08:18
2

This will definitely work:

print("\u0332".join("hello "))
Costa
  • 1,794
  • 1
  • 12
  • 21
1

You may type

print("\u0332".join("hello ")) enter image description here

WenHao1223
  • 29
  • 3
1

Struggled with this issue for a bit, and in my case the problem was the line 'colorama.init(autoreset=True)'. For some reason, it looks like in Python3 Windows, if you use the Colorama autoreset, you can't print underlined text (you can still print bold text, with foreground, background colors, etc., so far I found that only the underline formatting is affected). Of course, that also means you would have to reset the text manually by adding the ANSI reset sequence ESC [0m (in the example below, that would be the \033[0m at the end of the print statement).

import colorama
#colorama.init(autoreset=True) #This line was causing me problems
#import os #To use below line
#os.system("") #This line can make it work in some terminals
print("\033[1;4mBold and underlined text\033[0m")

Some additional notes, after some more testing:

  • This might still not work on some terminals. For example, I first tried this on the VSC (Visual Studio Code) built-in terminal and it worked, but then I tried it on cmd and it didn't work.
  • On terminals in which this doesn't work, adding the os.system("") line before the print statement can make it work. I tested this on cmd, and it worked (line colorama.init(autoreset=True) still commented).

EDIT: Added 'import colorama' line and additional notes for completeness

Luciano P.
  • 39
  • 3
0
string = 'Hello world'
emptystring = ''

for i in range(0, len(string)):

    if string[i] == ' ':
        emptystring = emptystring + string[i]
    else:
        emptystring= emptystring+string[i]+str('\u0332')

print(emptystring)

Here is the output

Uday
  • 1
  • 1
0

make sure you have python3 interpreter then run

x="the solution"
print("\033[4m" + x + "\033[0m")
0

Make sure you have Python 3.6+ Install quo using pip https://pypi.org/project/quo

from quo import echo
echo(f"Hello World!!!", underline=True)