Skip to main content


Python underline string, Python underline text, Underline python print

underline python print, underline text, underline text python, python underline text, python underscore, python underline string, python undersocre in string, python undersocre in number, python underscore functuion argument

Hello everyone,
As the topic suggest we are going to create python output that would be simple output in an underline form itself inside python IDE.

Lets get on with the code:-


>>> print("\u0332".join("hello "))
h̲e̲l̲l̲o̲ 

>>> text = "Hello"
>>> print("\u0332".join(text))
H̲e̲l̲l̲o

>>> text = "Hello "
>>> print("\u0332".join(text))
H̲e̲l̲l̲o̲ 

>>> text = "Hello world I am a python developer"
>>> print("\u0332".join(text))
H̲e̲l̲l̲o̲ ̲w̲o̲r̲l̲d̲ ̲I̲ ̲a̲m̲ ̲a̲ ̲p̲y̲t̲h̲o̲n̲ ̲d̲e̲v̲e̲l̲o̲p̲e̲r

As you would notice here we are using "\u0332" which is a Unicode for underline.
But as we use a variable it does not print proper underline under every words.

Now we will use for i in text:-

>>> text = "Hello world I am a python developer"
>>> for i in text:
 print(i)

 
H
e
l
l
o
 
w
o
r
l
d
 
I
 
a
m
 
a
 
p
y
t
h
o
n
 
d
e
v
e
l
o
p
e
r

Ok now there is a problem.... But first let us add underline Unicode and lets see what happens.

>>> text = "Hello world I am a python developer"
>>> for i in text:
 print(i+"\u0332")

 
H̲
e̲
l̲
l̲
o̲
 ̲
w̲
o̲
r̲
l̲
d̲
 ̲
I̲
 ̲
a̲
m̲
 ̲
a̲
 ̲
p̲
y̲
t̲
h̲
o̲
n̲
 ̲
d̲
e̲
v̲
e̲
l̲
o̲
p̲
e̲
r̲
>>> 


Now we will make the horizontal output to lateral output. To do this all we have to do is add (end = ' ')

>>> text = "Hello world I am a python developer"
>>> for i in text:
 print(i+"\u0332",end='')

 
H̲e̲l̲l̲o̲ ̲w̲o̲r̲l̲d̲ ̲I̲ ̲a̲m̲ ̲a̲ ̲p̲y̲t̲h̲o̲n̲ ̲d̲e̲v̲e̲l̲o̲p̲e̲r̲

But still there is a problem. At some places there are double underline. To solve this we will use isspace() and if else statement

>>> text = "Hello world I am a python developer"
>>> for i in text:
 if i.isspace() == False:
  print(i+underline,end='')
 else:
  print(i,end='')

  
H̲e̲l̲l̲o̲ w̲o̲r̲l̲d̲ I̲ a̲m̲ a̲ p̲y̲t̲h̲o̲n̲ d̲e̲v̲e̲l̲o̲p̲e̲r̲

If you want Underline + text on every line then just remove end=' '  from the else statement

>>> text = "Hello world I am a python developer"
>>> for i in text:
 if i.isspace() == False:
  print(i+underline,end='')
 else:
  print(i)

  
H̲e̲l̲l̲o̲ 
w̲o̲r̲l̲d̲ 
I̲ 
a̲m̲ 
a̲ 
p̲y̲t̲h̲o̲n̲ 
d̲e̲v̲e̲l̲o̲p̲e̲r̲

Other way of doing it using python bytes but its almost the same and also using input method.

>>> ### Code_1
>>> ur_string = input("Type something:- ")
Type something:- Hello World I am Your Boss
>>> underline_byte = b'\xcc\xb2'
>>> underline = str(underline_byte,'utf-8')
>>> for x in ur_string:
 if x.isspace() == False:
  print(x+underline,end='')
 else:
  print(x,end='')

  
H̲e̲l̲l̲o̲ W̲o̲r̲l̲d̲ I̲ a̲m̲ Y̲o̲u̲r̲ B̲o̲s̲s̲

>>> ########################################

>>> ### Code_2
>>> ur_string = input("Type something:- ")
Type something:- Hello World I am Your Best Friend
>>> underline_byte = b'\xcc\xb2'
>>> underline = str(underline_byte,'utf-8')
>>> for x in ur_string:
 if x.isspace() == False:
  print(x+underline,end='')
 else:
  print(x)

  
H̲e̲l̲l̲o̲ 
W̲o̲r̲l̲d̲ 
I̲ 
a̲m̲ 
Y̲o̲u̲r̲ 
B̲e̲s̲t̲ 
F̲r̲i̲e̲n̲d̲
>>> 

Hope you like the tutorial.

Python is simple and fun to play with and yet very powerful. 🔥
Run Web Story using Python Server


🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍

Popular Posts

Python pip - Installing modules from IDLE (Pyton GUI) for python 3.7

Top 40 Python - String Processing in Python | Working with String in Python

Top 11 Essential Python Tips and Tricks

Python Program - When was I born? / Date of Birth / MY BIRTHDAY (using Python3+)





Subscribe to our Channel


Follow us on Facebook Page

Join our python facebook groups



Join us on Telegram