I have a python string a = "Name:john KES:50 code:5234", how can I go through the string (a) to get the list output b = ["john", 50, 5234], keeping the order ie.
Asked
Active
Viewed 681 times
-1
-
we can do this with regex expressions if all expressions are of that from.use re.findall() – Rahul A Ranger Oct 04 '20 at 14:13
1 Answers
1
You can try the following code:
a = "Name:john KES:50 code:5234"
a = a.split(" ")
ls=[]
for i in a:
c = i.split(':')[1]
if c.isdigit():
c = int(c)
ls.append(c)
else:
ls.append(c)
ls

Surya Lohia.
- 446
- 4
- 16