0

On taking the input 1,2,3,4,5 initially python will consider it as a string datatype,

But if we try to convert the numbers to integer using int() we'll encounter another problem as a commas cannot be converted into integer format

What will be the most appropriate way to convert the numbers into integer and keep the commas in the string format itself

Note: input cannot be changed in anyways

Ctrl7
  • 161
  • 2
  • 12
  • Duplicate of https://stackoverflow.com/questions/3477502/pythonic-method-to-parse-a-string-of-comma-separated-integers-into-a-list-of-i – mkrieger1 Sep 06 '21 at 07:41
  • Does this answer your question? ["pythonic" method to parse a string of comma-separated integers into a list of integers?](https://stackoverflow.com/questions/3477502/pythonic-method-to-parse-a-string-of-comma-separated-integers-into-a-list-of-i) – cigien Sep 06 '21 at 23:07

1 Answers1

2

try this:

list(map(int, input().split()))

Output:

# input : 1 2 3 4 5
[1, 2, 3, 4, 5]

Or:

str_num = "1,2,3,4,5"
list(map(int, str_num.split(',')))

Output:

[1, 2, 3, 4, 5]
I'mahdi
  • 23,382
  • 5
  • 22
  • 30