Home > Mobile >  How to print f-strings and other items simultaneously but each item be seperated by atleast one line
How to print f-strings and other items simultaneously but each item be seperated by atleast one line

Time:11-18

So as you can guess from the title, I want to print multiple items be it variables or f-strings on separate lines simultaneously i.e. without having to print each item on its own.

print(
    f'Revenue as stated in the General Journal is ${a}',
    f'Revenue as stated in the Transaction Table is ${b}',
    f'Revenue is overstated by ${c}',
    rt.info(),
    rtrtl.info(),
    rt.head(10),
    rtrtl.head(10)
    )

The current output

Revenue as stated in the General Journal is $2,079,839.55 Revenue as stated in the Transaction Table is $2,238,120.00 Revenue is overstated by $158,280.45

The desired output

Revenue as stated in the General Journal is $2,079,839.55

Revenue as stated in the Transaction Table is $2,238,120.00

Revenue is overstated by $158,280.45

CodePudding user response:

If want new lines, add '\n' to end of the fstring with sep='' or use sep='\n'. Also, to format the float with commas in output need to add ',' as format modifier to the f-string component.

a = 2_079_839.55 
b = 2_238_120.00
c = 158_280.45

print(
    f'Revenue as stated in the General Journal is ${a:,}',
    f'Revenue as stated in the Transaction Table is ${b:,}',
    f'Revenue is overstated by ${c:,}', sep='\n\n')

Output:

Revenue as stated in the General Journal is $2,079,839.55

Revenue as stated in the Transaction Table is $2,238,120.0

Revenue is overstated by $158,280.45

CodePudding user response:

Consider print()-ing the values in a loop; without other args, they'll go to stdout, which is already buffered so successive print() calls without explicit flush=True won't be realistically different to one big call

lines = (
    f'Revenue as stated in the General Journal is ${a}',
    f'Revenue as stated in the Transaction Table is ${b}',
    f'Revenue is overstated by ${c}',
    rt.info(),
    rtrtl.info(),
    rt.head(10),
    rtrtl.head(10)
)

for line in lines:
    print(line)

CodePudding user response:

You can use triple quoting with F-strings eg::

print(
    f"""Revenue as stated in the General Journal is ${a}

    Revenue as stated in the Transaction Table is ${b}

    Revenue is overstated by ${c}"""
    )
  • Related