B
B
barakuda12021-12-16 20:10:26
Python
barakuda1, 2021-12-16 20:10:26

How to display many variables correctly in print in python?

Hello.

Python 3.9.1

How to output properly?

a = 1
b = 2
c = 3
d = 'sss'

print('D = %s, C = %d, B = %d, A = %d' % d % c % b % a)


Traceback (most recent call last):
  File "<string>", line 6, in <module>
TypeError: not enough arguments for format string
>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Nesterov, 2021-12-16
@AlexNest

import time

def main() -> None:
    a = 1
    b = 2
    c = 3
    d = 4
    e = 5
    some_string = f'{a} {b} {c} {d} {e}'
    print(some_string)

if __name__ == '__main__':
    main()
    time.sleep(10)

PS: Option 2:
def main_2() -> None:
    a = 1
    b = 2
    c = 3
    d = 4
    e = 5
    some_string = f'{a} {b} {c} {d} {e}'
    print(a, b, c, d, e)

S
soremix, 2021-12-16
@SoreMix

Since python 3.8+, then
print(f'{a=} {b=} {c=} {d=}')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question