Python: String formatter to get underlined headline Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceWhat is the difference between String and string in C#?Calling an external command in PythonWhat are metaclasses in Python?How do I read / convert an InputStream into a String in Java?Does Python have a ternary conditional operator?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?Does Python have a string 'contains' substring method?How do I lowercase a string in Python?Why is char[] preferred over String for passwords?

Is it possible to ask for a hotel room without minibar/extra services?

What computer would be fastest for Mathematica Home Edition?

Is above average number of years spent on PhD considered a red flag in future academia or industry positions?

Mortgage adviser recommends a longer term than necessary combined with overpayments

Do we know why communications with Beresheet and NASA were lost during the attempted landing of the Moon lander?

Windows 10: How to Lock (not sleep) laptop on lid close?

If A makes B more likely then B makes A more likely"

Classification of bundles, Postnikov towers, obstruction theory, local coefficients

Why is there no army of Iron-Mans in the MCU?

What's the point in a preamp?

3 doors, three guards, one stone

Biased dice probability question

Can a zero nonce be safely used with AES-GCM if the key is random and never used again?

Area of a 2D convex hull

What's the difference between (size_t)-1 and ~0?

New Order #5: where Fibonacci and Beatty meet at Wythoff

Can a monk deflect thrown melee weapons?

What was the last x86 CPU that did not have the x87 floating-point unit built in?

Writing Thesis: Copying from published papers

What are the performance impacts of 'functional' Rust?

Why use gamma over alpha radiation?

Complexity of many constant time steps with occasional logarithmic steps

Fishing simulator

Losing the Initialization Vector in Cipher Block Chaining



Python: String formatter to get underlined headline



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceWhat is the difference between String and string in C#?Calling an external command in PythonWhat are metaclasses in Python?How do I read / convert an InputStream into a String in Java?Does Python have a ternary conditional operator?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?Does Python have a string 'contains' substring method?How do I lowercase a string in Python?Why is char[] preferred over String for passwords?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















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(":sn:sn".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:sn?????n".format("This is an underlined headline.", "-"))


Thank you :)










share|improve this question






















  • 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 8 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 8 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 8 at 14:58






  • 2





    @Max16hr If you want to do the same thing 5 times, write a function.

    – khelwood
    Mar 8 at 15:04






  • 1





    if you don't want to create additional names use print((lambda s: '%sn%s' % (s, '-' * len(s)))("This is an underlined headline."))

    – panda-34
    Mar 8 at 16:24

















2















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(":sn:sn".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:sn?????n".format("This is an underlined headline.", "-"))


Thank you :)










share|improve this question






















  • 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 8 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 8 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 8 at 14:58






  • 2





    @Max16hr If you want to do the same thing 5 times, write a function.

    – khelwood
    Mar 8 at 15:04






  • 1





    if you don't want to create additional names use print((lambda s: '%sn%s' % (s, '-' * len(s)))("This is an underlined headline."))

    – panda-34
    Mar 8 at 16:24













2












2








2








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(":sn:sn".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:sn?????n".format("This is an underlined headline.", "-"))


Thank you :)










share|improve this question














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(":sn:sn".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:sn?????n".format("This is an underlined headline.", "-"))


Thank you :)







python string printing format python-3.7






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 14:49









Max16hrMax16hr

4016




4016












  • 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 8 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 8 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 8 at 14:58






  • 2





    @Max16hr If you want to do the same thing 5 times, write a function.

    – khelwood
    Mar 8 at 15:04






  • 1





    if you don't want to create additional names use print((lambda s: '%sn%s' % (s, '-' * len(s)))("This is an underlined headline."))

    – panda-34
    Mar 8 at 16:24

















  • 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 8 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 8 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 8 at 14:58






  • 2





    @Max16hr If you want to do the same thing 5 times, write a function.

    – khelwood
    Mar 8 at 15:04






  • 1





    if you don't want to create additional names use print((lambda s: '%sn%s' % (s, '-' * len(s)))("This is an underlined headline."))

    – panda-34
    Mar 8 at 16:24
















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 8 at 14:55





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 8 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 8 at 14:56





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 8 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 8 at 14:58





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 8 at 14:58




2




2





@Max16hr If you want to do the same thing 5 times, write a function.

– khelwood
Mar 8 at 15:04





@Max16hr If you want to do the same thing 5 times, write a function.

– khelwood
Mar 8 at 15:04




1




1





if you don't want to create additional names use print((lambda s: '%sn%s' % (s, '-' * len(s)))("This is an underlined headline."))

– panda-34
Mar 8 at 16:24





if you don't want to create additional names use print((lambda s: '%sn%s' % (s, '-' * len(s)))("This is an underlined headline."))

– panda-34
Mar 8 at 16:24












2 Answers
2






active

oldest

votes


















1














Maybe try using ANSI escape sequences



class Format:
end = '33[0m'
underline = '33[4m'

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


It will print out underlined text, for the whole ANSI escepe sequence documetation click here






share|improve this answer























  • I allready tried this but it gives me just [0mThis is an underlined headline.[4m

    – Max16hr
    Mar 8 at 15:02












  • Could you post here the code trying it like this?

    – Aeossa
    Mar 8 at 15:06






  • 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 8 at 15:08






  • 1





    I've never really used Eclipse. But this might help

    – Aeossa
    Mar 8 at 15:13






  • 1





    Ah! In the preferences I have to disable "Use Windows color mapping". Now it is working! :)

    – Max16hr
    Mar 8 at 15:28


















0














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.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55065640%2fpython-string-formatter-to-get-underlined-headline%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Maybe try using ANSI escape sequences



    class Format:
    end = '33[0m'
    underline = '33[4m'

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


    It will print out underlined text, for the whole ANSI escepe sequence documetation click here






    share|improve this answer























    • I allready tried this but it gives me just [0mThis is an underlined headline.[4m

      – Max16hr
      Mar 8 at 15:02












    • Could you post here the code trying it like this?

      – Aeossa
      Mar 8 at 15:06






    • 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 8 at 15:08






    • 1





      I've never really used Eclipse. But this might help

      – Aeossa
      Mar 8 at 15:13






    • 1





      Ah! In the preferences I have to disable "Use Windows color mapping". Now it is working! :)

      – Max16hr
      Mar 8 at 15:28















    1














    Maybe try using ANSI escape sequences



    class Format:
    end = '33[0m'
    underline = '33[4m'

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


    It will print out underlined text, for the whole ANSI escepe sequence documetation click here






    share|improve this answer























    • I allready tried this but it gives me just [0mThis is an underlined headline.[4m

      – Max16hr
      Mar 8 at 15:02












    • Could you post here the code trying it like this?

      – Aeossa
      Mar 8 at 15:06






    • 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 8 at 15:08






    • 1





      I've never really used Eclipse. But this might help

      – Aeossa
      Mar 8 at 15:13






    • 1





      Ah! In the preferences I have to disable "Use Windows color mapping". Now it is working! :)

      – Max16hr
      Mar 8 at 15:28













    1












    1








    1







    Maybe try using ANSI escape sequences



    class Format:
    end = '33[0m'
    underline = '33[4m'

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


    It will print out underlined text, for the whole ANSI escepe sequence documetation click here






    share|improve this answer













    Maybe try using ANSI escape sequences



    class Format:
    end = '33[0m'
    underline = '33[4m'

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


    It will print out underlined text, for the whole ANSI escepe sequence documetation click here







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 8 at 14:58









    AeossaAeossa

    417




    417












    • I allready tried this but it gives me just [0mThis is an underlined headline.[4m

      – Max16hr
      Mar 8 at 15:02












    • Could you post here the code trying it like this?

      – Aeossa
      Mar 8 at 15:06






    • 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 8 at 15:08






    • 1





      I've never really used Eclipse. But this might help

      – Aeossa
      Mar 8 at 15:13






    • 1





      Ah! In the preferences I have to disable "Use Windows color mapping". Now it is working! :)

      – Max16hr
      Mar 8 at 15:28

















    • I allready tried this but it gives me just [0mThis is an underlined headline.[4m

      – Max16hr
      Mar 8 at 15:02












    • Could you post here the code trying it like this?

      – Aeossa
      Mar 8 at 15:06






    • 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 8 at 15:08






    • 1





      I've never really used Eclipse. But this might help

      – Aeossa
      Mar 8 at 15:13






    • 1





      Ah! In the preferences I have to disable "Use Windows color mapping". Now it is working! :)

      – Max16hr
      Mar 8 at 15:28
















    I allready tried this but it gives me just [0mThis is an underlined headline.[4m

    – Max16hr
    Mar 8 at 15:02






    I allready tried this but it gives me just [0mThis is an underlined headline.[4m

    – Max16hr
    Mar 8 at 15:02














    Could you post here the code trying it like this?

    – Aeossa
    Mar 8 at 15:06





    Could you post here the code trying it like this?

    – Aeossa
    Mar 8 at 15:06




    2




    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 8 at 15:08





    @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 8 at 15:08




    1




    1





    I've never really used Eclipse. But this might help

    – Aeossa
    Mar 8 at 15:13





    I've never really used Eclipse. But this might help

    – Aeossa
    Mar 8 at 15:13




    1




    1





    Ah! In the preferences I have to disable "Use Windows color mapping". Now it is working! :)

    – Max16hr
    Mar 8 at 15:28





    Ah! In the preferences I have to disable "Use Windows color mapping". Now it is working! :)

    – Max16hr
    Mar 8 at 15:28













    0














    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.






    share|improve this answer



























      0














      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.






      share|improve this answer

























        0












        0








        0







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 9 at 9:32









        snakecharmerbsnakecharmerb

        12.1k42552




        12.1k42552



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55065640%2fpython-string-formatter-to-get-underlined-headline%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко