0

I am looking for something similar in python print in printf c. Is there something like that in python ? printf("%2$c %1$c", a, b).

Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
Jimmy Gonçalves
  • 79
  • 1
  • 2
  • 9

2 Answers2

2

str.format is designed to repeat arguments or change their order when passing a position (starting at 0):

a = 10
b = 20
print("{1} {0}".format(a,b))

you get:

20 10
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
0

For completeness, the % operator lets you select elements from a dict, although the format method is still the better way to go.

>>> print("%(two)s %(one)s" % {'one': 1, 'two': 2})
2 1
>>> print("%(one)s %(two)s" % {'one': 1, 'two': 2})
1 2
chepner
  • 497,756
  • 71
  • 530
  • 681