简体   繁体   中英

Trying to create a list & tuple from User Input

For eg 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?

You should do

z = x.split(",")

instead of

z = x.split()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM