Python-courses-python-exercises-games-Python-courses-

Exercise 50

Write a python program as a function which takes a string s as a parameter and returns the list of numeric characters contained in the string s. Example if s = 'Python 3.0, released in 2008, and completely revised in 2020' the function returns the list: [3, 0, 2, 0, 0, 8, 2, 0, 2, 0].

Solution

def extractNumbers(s):
# initializing extracted list numbers
L = []

for x in s:
if x.isnumeric():
x = int(x)
L.append(x)
return L
s = 'Python 3.0, released in 2008, and completely revised in 2020'
print(extractNumbers(s))
# The output is : [3, 0, 2, 0, 0, 8, 2, 0, 2, 0]

Younes Derfoufi
my-courses.net

Leave a Reply