Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print(f"...:")-statement too long - break it into multiple lines without messing up the format

I have a console program with formatted output. to always get the same length of the printout, I have a rather complex formatted print output.

print(f"\n{WHITE_BG}{64*'-'}")
print(f"\nDirektvergleich{9*' '}{RED}{players[0].name}{4*' '}{GREEN}vs.{4*' '}{RED}{players[1].name}{CLEAR}\n")
print(f"""{15*'~'}{' '}{YELLOW}Gesamt{CLEAR}:{' '}{players[0].name}{' '}{GREEN}{int(player1_direct_wins)}{(int(4-len(player1_direct_wins)))*' '}-{(int(4-len(player1_direct_losses)))*' '}{int(player1_direct_losses)}{CLEAR}{' '}{players[1].name}{' '}{(28-len(players[0].name)-len(players[1].name))*'~'}\n""")
print(f"""{15*'~'}{' '}{YELLOW}Trend{CLEAR}:{'  '}{players[0].name}{' '}{GREEN}{int(player1_trend_wins)}{(int(4-len(player1_trend_wins)))*' '}-{(int(4-len(player1_trend_losses)))*' '}{int(player1_trend_losses)}{CLEAR}{' '}{players[1].name}{' '}{(28-len(players[0].name)-len(players[1].name))*'~'}""")    
print(f"\n{WHITE_BG}{64*'-'}")

This leads to the following output in my windows cmd

enter image description here

For readibility purpose, I tried to make the print over multiple lines, therefore I found on stackoverflow the idea to start with triple quotes. But when I cut this print(f"...") statement in the middle, I mess up my formatting.

Example:

print(f"\n{WHITE_BG}{64*'-'}") #als String einspeisen?!
print(f"\nDirektvergleich{9*' '}{RED}{players[0].name}{4*' '}{GREEN}vs.{4*' '}{RED}{players[1].name}{CLEAR}\n")
print(f"""{15*'~'}{' '}{YELLOW}Gesamt{CLEAR}:{' '}{players[0].name}{' '}{GREEN}{int(player1_direct_wins)}{(int(4-len(player1_direct_wins)))*' '}-
    {(int(4-len(player1_direct_losses)))*' '}{int(player1_direct_losses)}{CLEAR}{' '}{players[1].name}{' '}{(28-len(players[0].name)-len(players[1].name))*'~'}\n""")
print(f"""{15*'~'}{' '}{YELLOW}Trend{CLEAR}:{'  '}{players[0].name}{' '}{GREEN}{int(player1_trend_wins)}{(int(4-len(player1_trend_wins)))*' '}-
    {(int(4-len(player1_trend_losses)))*' '}{int(player1_trend_losses)}{CLEAR}{' '}{players[1].name}{' '}{(28-len(players[0].name)-len(players[1].name))*'~'}""")       
print(f"\n{WHITE_BG}{64*'-'}")

leads to...

enter image description here

can anyone point me in the right direction how to format my output in the displayed way, but without having this absurd long line length.

Thank you guys in advance!

like image 526
Cut7er Avatar asked Dec 07 '25 10:12

Cut7er


1 Answers

Triple quoted strings preserve newline characters, so they are indeed not what you want here. Now when it finds two adjacent strings, the Python parser automagically concatenates them into a single string, i.e.:

s = "foo" "bar"

is equivalent to

s = "foobar"

And this works if you put your strings within parens:

s = ("foo" "bar")

in which case you can put each string on its own line as well:

s = (
    "foo"
    "bar"
    )

This also applies to "fstrings" so what you want is something like:

print((
    f"{15*'~'}{' '}{YELLOW}Gesamt{CLEAR}:{' '}{players[0].name}{' '}{GREEN} " 
    f"{int(player1_direct_wins)}{(int(4-len(player1_direct_wins)))*' '}-"
    f"{(int(4-len(player1_direct_losses)))*' '}{int(player1_direct_losses)}"
    f"{CLEAR}{' '}{players[1].name}{' '}{(28-len(players[0].name)-"
    f"len(players[1].name))*'~'}\n"
    ))

That being said, I'd rather use intermediate variables than trying to cram such complex expressions in a fstring.

like image 135
bruno desthuilliers Avatar answered Dec 10 '25 05:12

bruno desthuilliers