-3

Is it possible to print a string before using the * operator when unpacking a tuple:

m = ['b', 'a', 'e']
print(*m, sep = ',')

b, a, e

I tried to print something before it:

print("String: " + *m, sep = ",")

My desired output would be:

String: b, a, e

Is it possible to have a string print before this, and what would be the correct syntax?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
ter123
  • 21
  • 7

3 Answers3

1

*m unpacks the list m into separate arguments. It's equivalent to:

print('b', 'a', 'e')

You can add additional arguments before and after that:

print('string', *m, sep=',')

is it possible to print that without the comma being after the string, and only having the comma apply to the items in the list?

Take your pick:

print(f'String: {", ".join(m)}')

print('String:', ', '.join(m), sep=' ')

print('String:', end=' ')
print(*m, sep=', ')
deceze
  • 510,633
  • 85
  • 743
  • 889
0

Try using this code:

print(",".join(m))
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
kaamkar
  • 11
  • 1
  • It helps more if you supply an explanation why this is the preferred solution and explain how it works. We want to educate, not just provide code. – the Tin Man May 30 '20 at 00:46
-2

I think you can use

print(",".join(m))
the Tin Man
  • 158,662
  • 42
  • 215
  • 303