separation between string and integer | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

separation between string and integer

Hi ✨ I need help in making a function that do like this : ... s="Af345gOh18" func(s) ... output: [345,18] ... suggest me haw i can do this ...thiss thing is very hard.. i was traying from day and 0 result 😅

22nd May 2021, 5:07 PM
Moha Riad
Moha Riad - avatar
4 Answers
+ 1
Dude you should always include your code attempt, even if it doesn't make sense. We can help you write the code yourself by pointing out errors. Anyway, if you tried and got stuck, this should work: def numSep(str): arr = list(str) for elem in arr: if elem.isalpha(): arr[arr.index(elem)] = " " numArr = "".join(arr).split() for elem in numArr: numArr[numArr.index(elem)] = int(elem) print(numArr) numSep("Af345gOh18") Output: [345,18] Cheers✌
22nd May 2021, 8:01 PM
Isaac Fernandes
Isaac Fernandes - avatar
0
# with regex and list comprehension: import re print([int(s) for s in re.findall(r"\d+")])
22nd May 2021, 5:58 PM
visph
visph - avatar
0
Isaac Fernandes your solution still not working (I tested your previously attempt posted about 2 hours ago, that you've deleted just after)... it output: ["345","18"] you must then convert each string to numbers ^^
22nd May 2021, 8:13 PM
visph
visph - avatar
0
Check now.
22nd May 2021, 8:18 PM
Isaac Fernandes
Isaac Fernandes - avatar