Search code examples
pythontuplesfwrite

Multiple %s with list/tuple in Python


I am trying to output the same string from a list/tuple to different %s, I tried using this:

A = ('A', 'B', 'C', 'D')
# A = list(('A', 'B', 'C', 'D')
print type(A)
output = open('output.txt', 'w')
output.writelines('Multiple outputs like %s and %s\n' % (f for f in A, f for f in A))
output.close()

How is it done in Python?

But it produces a syntax error. I need an output of the form:

Multiple outputs like A and A
Multiple outputs like B and B
Multiple outputs like C and C
Multiple outputs like D and D

Solution

  • This should work:

    output.writelines(('Multiple outputs like %s and %s\n' % (f,f)) for f in A)