Python-courses-neighborhood-Python-courses-bin-Python-courses-python-string

Exercise 62

Write a Python program that determines the list of digits within a given string. Example if s = "Python3.8 is more powerful than Python2.7 " the program must returns the list :

['3', '8', '2', '7']

Solution

def digit(s):
# initializing the list of digits within the string s.
digitList = []

# iterate over all characters of s:
for x in s:
if x.isdigit():
digitList.append(x)

return digitList

#Testing algorithm:
s = "Python3.8 is more powerful than Python2.7 "
print("List of digits within the string s :" , digit(s))
# The output is :
# List of digits within the string s : ['3', '8', '2', '7']

Younes Derfoufi
my-courses.net

Leave a Reply