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).
Asked
Active
Viewed 74 times
0

Alan Kavanagh
- 9,425
- 7
- 41
- 65

Jimmy Gonçalves
- 79
- 1
- 2
- 9
-
1you want `str.format`: `"{1} {0}".format(a,b)` right? – Jean-François Fabre Oct 23 '17 at 11:48
-
1`print('%s %s' % (b, a))` – Alan Kavanagh Oct 23 '17 at 11:49
2 Answers
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