-1

I was wondering how I would take out the whitespace in a zip function.

So for example if my code is:

for a,b,c, in zip(firstlist,secondlist,thirdlist):
    print(a,b,c)

I'm left with a space between a and b as well as between b and c. I was wondering how I would get rid of that.

NewOne
  • 11
  • 1

2 Answers2

1

print(str(a)+str(b)+str(c)) instead of print(a,b,c). This will concatenate the values in one string, while now it prints a, b, c as elements of a tuple.

blue_note
  • 25,410
  • 6
  • 56
  • 79
  • If OP is asking what you think he is asking then this is not the way to go. – styvane Jan 28 '17 at 12:59
  • I think he probably doesn't realise the difference between tuple and string while printing, and I'm providing enough hints for him to find out. Anyway, that's they way I would think of doing it, I'm open to other ideas – blue_note Jan 28 '17 at 13:04
0

I know you've already accepted an answer, but I think you'd be better off taking control for the format you're printing as follows:

list1 = ["a","b","c"]
list2 = ["A","B","C"]
list3 = ["1","2","3"]

for a,b,c in zip(list1, list2, list3):
    print "%s %s %s" % (a, b, c)

The string formatting allows you to control exactly how you want the output to look. For example, if you want commas and not spaces you change the code to...

for a,b,c in zip(list1, list2, list3):
    print "%s%s%s" % (a, b, c)

What my code is saying is...

"Using a string format of "%s%s%s" that expects three strings, plug in the values that you'll find in the tuple (a, b, c)"

Your original code was printing the string representation of a tuple. When you print a tuple, it happens to print with brackets and commas...

Printing tuple with string formatting in Python

... which is coincidentally similar to the format you wanted.

Community
  • 1
  • 1
Martin Peck
  • 11,292
  • 2
  • 40
  • 68