0

For e.g. user input:

4,7,5,33,2,8

should give output like this:

['4', '7', '5', '33', '2', '8'] ('4', '7', '5', '33', '2', '8')

So far i have this:

x = input()
z = x.split()
y = tuple(z)
print(z, y)

why there is extra , in the end of tuple?

5
  • Input: 12,56,48,86 Output: ['12,56,48,86'] ('12,56,48,86',) why there is extra , in the end of tuple? Commented Apr 8, 2020 at 13:47
  • It represents that it is a tuple. ('...') is just wrapped str. ('...',) is tuple of str. Commented Apr 8, 2020 at 13:50
  • z = x.split(',') Commented Apr 8, 2020 at 13:51
  • And x.split() is splitting the string by white spaces. You should use x.split(','). Commented Apr 8, 2020 at 13:51
  • 2
    This ('12,56,48,86',) is tuple containing one element....this ('4', '7', '5', '33', '2', '8') is tuple containing 5 elements. Commented Apr 8, 2020 at 13:55

2 Answers 2

2

You should do

z = x.split(",")

instead of

z = x.split()
Sign up to request clarification or add additional context in comments.

Comments

1

The extra comma is because the string you tried to split is not what you are expecting after the split. Since the split was performed on space with x.split() and string does not have white space so after the step the string is still a single string and then list and tuple just wrap the string and that's where the extra comma is coming from. Example tuple('a') == ('a',)

x.split(',') will create a list separate by comma and will fetch you expected results

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.