Python Forum

Full Version: Using .join()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there,

I'm supposed to join up the string by entering an input. I can't seem to get it to work, I suspect the separator i input might not be a string. Any advice?

phrase_words = ['Jack', 'and', 'Jill', 'went', 'up', 'the', 'hill', 'To', 'fetch', 'a', 'pail', 'of', 'water']

separator = input("Enter: ")

print(separator.join(phrase_words))
Thanks
(Jun-26-2020, 06:40 AM)extricate Wrote: [ -> ]I can't seem to get it to work, I suspect the separator i input might not be a string. Any advice?

What exactly doesn't work? input returns a string, so I don't know what you mean.
Weird, I put the code into visualizer and it worked.

Guess my jupyter has some problem running the code

The error code was:

Enter: -
File "<string>", line 1
&
^
SyntaxError: unexpected EOF while parsing
Are you by any chance using Python 2? I suspect so, as input behaves like eval in Python 2, so it's trying to evaluate what you enter as Python code. If you are using Python 2, why? It has been end of life since the beginning of the year.