-5

I want to search my string for single digits, for every different single digit in python. If it contains 1 in want to add 1 to another list and same for other digits.

  //single-digit found
 ar.append(single-digit)

example-string: ap126ks8

 ar=[1,2,6,8]
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
Jai dewani
  • 143
  • 1
  • 9

2 Answers2

0

Try this

strr="ap126ks8"
ar=[]
for i in strr:
    if i.isdigit():
        ar.append(i)

Or using list comprehension

strr="ap126ks8"
ar=[i  for i in strr if i.isdigit()]

output

['1', '2', '6', '8']

Artier
  • 1,648
  • 2
  • 8
  • 22
0

A Simpler Way Is To Create A List.

example-string = "ap126ks8ar"
ar = []
numbers = "0123456789"

i=0
while i< len(example-string):
    if example-string[i] in numbers :
         ar.append[example-string[i]
    i+=1