Dhruv Nautiyal
Dhruv Nautiyal

Reputation: 129

Trying to create a list & tuple from User Input

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?

Upvotes: 0

Views: 260

Answers (2)

mad_
mad_

Reputation: 8273

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

Upvotes: 1

etnguyen03
etnguyen03

Reputation: 620

You should do

z = x.split(",")

instead of

z = x.split()

Upvotes: 2

Related Questions