4

To structure my console output, I want to print some information and I would like to start with an underlined headline. But how to do it nicely without creating an extra variable?

Right now I do it like this:

print("{:s}\n{:s}\n".format("This is an underlined headline.", len("This is an underlined headline.") * "-"))

what gives me the desired output:

This is an underlined headline.
-------------------------------

But that code is bad. Is there some better format string to achieve that?

print("{0:s}\n?????\n".format("This is an underlined headline.", "-"))

Thank you :)

Max16hr
  • 438
  • 2
  • 5
  • 20
  • What you have isn't that verbose to me, especially if you store the string, e.g. `s = "This is an underlined headline."; print('{}\n{}'.format(s, len(s)*'-'))` – Chris_Rands Mar 08 '19 at 14:55
  • As @Chris_Rands mentioned, it is not that verbose. You can call print two times to make it more readable, but that would require variable storing, which is also not that big of a deal – Rodolfo Donã Hosp Mar 08 '19 at 14:56
  • Yes, but creating a new variable just for 1 print? What if i have 5 sections, so i need 5 new variables? I hope there is something better. – Max16hr Mar 08 '19 at 14:58
  • 2
    @Max16hr If you want to do the same thing 5 times, write a function. – khelwood Mar 08 '19 at 15:04
  • If you want to do this 5 times, then just write a function: `def underlined(s): return f"{s}\n{'-'*len(s)}"`. You have one additional variable `s` but used 5 times over. – BoarGules Mar 08 '19 at 15:05
  • @khelwood: That's true. I dont want to do it 5 times XD So it would be only 1 variable. Would be okay. But I am searching for the smartest way. Maybe there is no better solution while using .fomat(). – Max16hr Mar 08 '19 at 15:07
  • 1
    if you don't want to create additional names use `print((lambda s: '%s\n%s' % (s, '-' * len(s)))("This is an underlined headline."))` – panda-34 Mar 08 '19 at 16:24
  • @panda-34: That is a good idea! – Max16hr Mar 08 '19 at 16:44

4 Answers4

5

Maybe try using ANSI escape sequences

class Format:
    end = '\033[0m'
    underline = '\033[4m'

print(Format.underline + 'Your text here' + Format.end)

It will print out underlined text, for the whole ANSI escape sequence documentation click here

vy32
  • 28,461
  • 37
  • 122
  • 246
Aeossa
  • 146
  • 1
  • 10
  • I allready tried this but it gives me just `[0mThis is an underlined headline.[4m` – Max16hr Mar 08 '19 at 15:02
  • 2
    @Max16hr That is because your terminal/console doesn't understand ANSI escape sequences. Most versions of Windows between Vista and very recent Windows 10 updates don't. – BoarGules Mar 08 '19 at 15:08
  • Maybe it is because I'm using Eclipse. So maybe there is some setting to make the console to understand ANSI. – Max16hr Mar 08 '19 at 15:10
  • 1
    I've never really used Eclipse. But [this](https://marketplace.eclipse.org/content/ansi-escape-console) might help – Aeossa Mar 08 '19 at 15:13
  • No, also did not work. Now, the ANSI strings seem to be interpreted, I get back my normal string `This is an underlined headline.` But it is still not underlined^^ – Max16hr Mar 08 '19 at 15:24
  • 1
    Ah! In the preferences I have to disable "Use Windows color mapping". Now it is working! :) – Max16hr Mar 08 '19 at 15:28
  • You can also use the `ansi` module ([link](https://pypi.org/project/ansi)) instead of writing an own class: `from ansi.colour.fx import underline, reset`. – Max16hr Mar 08 '19 at 15:57
  • This will help 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:19
2

There is a unicode character '\u0332', COMBINING LOW LINE*, which acts as an underline on the character that precedes it in a string. So you could try:

print('{:s}'.format('\u0332'.join('This is an underlined headline.')))

Which should produce an underlined string:

T̲h̲i̲s̲ ̲i̲s̲ ̲a̲n̲ ̲u̲n̲d̲e̲r̲l̲i̲n̲e̲d̲ ̲h̲e̲a̲d̲l̲i̲n̲e̲.

However the appearance of the output may depend on the application that renders the output, and the fonts it uses. My browser produces an underlined string, my (Linux) terminal displays it as if each character is followed by an underscore.

* There is also '\u0333', COMBINING DOUBLE LOW LINE, for double-underlining.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
  • 1
    Trying this again today, the terminal no longer displays the underscores at all. I'm not sure if this is a font issue or whether I was using a different computer when I wrote the answer originally. – snakecharmerb Aug 07 '21 at 15:41
0

snakecharmerb's answer works for me, but doesn't underline the last character, here is quick and inelegant fix:

def underline(input):
    return '{:s}'.format('\u0332'.join(input+' '))[:-1]
0
pip install simple-colors

from simple_colors import *
print(black('This is an underlined headline.', ['underlined']))

More options at https://pypi.org/project/simple-colors/

Me J
  • 1
  • 2