[Python] multi line string formatting in python

Discussão em 'Python' iniciado por Stack, Maio 26, 2025.

  1. Stack

    Stack Membro Participativo

    To begin with, I am working to a get a desired output like this:

    *********************************************************************
    hello
    *********************************************************************


    To achieve this I have assigned the desired output to a variable with multiline string and printing the same with format.

    $ cat varibale.py
    decorator = """ **********************************************************************
    {}
    ********************************************************************** """
    print(decorator.format("hello"))


    Output:

    **********************************************************************
    hello
    **********************************************************************


    The issue with above approach is the extra spaces in the third line of output which is looking odd.

    I am able to achieve this in the following way:

    $ cat varibale.py
    decorator = """ **********************************************************************
    {}
    *********************************************************************
    """
    print(decorator.format("hello"))


    Output:

    **********************************************************************
    hello
    *********************************************************************


    But this way my code doesn't look good, as it is not following the indentation.

    Please suggest the right way to achieve the desired output.

    Continue reading...

Compartilhe esta Página