11
print("hello")

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

1

7 Answers 7

33

You can do it by using escape characters.

print("\033[4mhello\033[0m")
3
2

This will definitely work:

print("\u0332".join("hello "))
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

0

You may type

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

2
  • This solution gives the following output to me: h_e_l_l_o Commented Sep 22, 2021 at 5:40
  • Yea this kind of works. I got one letter double underlined and one letter not underlined.
    – Taylrl
    Commented Jul 15, 2022 at 20:48
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

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) 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.