After user input, variable colors = "58, 234, 209", it is string. I need to convert it to list - colors = [58, 234, 209]. Which way I can do it. Thank you.
color = ("Enter color you like, something like that 58, 234, 209. ")
colors = input(color)
After user input, variable colors = "58, 234, 209", it is string. I need to convert it to list - colors = [58, 234, 209]. Which way I can do it. Thank you.
color = ("Enter color you like, something like that 58, 234, 209. ")
colors = input(color)
colors = colors.split()
This will split on each of the spaces in the string
Since you have some non-space characters in there like ,
, you'll want to use a flexible splitter, like re.split
:
import re
colors = input('Enter color you like, something like that 58, 234, 209. ')
colors = list(map(int, re.split(r'[^\d]+', colors)))