0

I need to split this string into specific word order. I need to add comma after each word. I'm completely new to python. This is the closest I have got, but it adds space after every comma. How can I get rid of the extra space?

words = ('Hann Tumi fer á fætur').split()

print(words[3],',',words[0],',',words[2],',',words[4],',',words[1])

result:

á , Hann , fer , fætur , Tumi
roganjosh
  • 12,594
  • 4
  • 29
  • 46

1 Answers1

0

To remove the spaces add sep='' to the end of your print:

print(words[3],',',words[0],',',words[2],',',words[4],',',words[1], sep='')

The reason the spaces occur is due to the separator being a space by default for each argument in the print function, adding sep='' tells it to make the separator to an empty string.

Output:

á,Hann,fer,fætur,Tumi
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64