Python Project: Find all integers in a string and find the sum.

Task

Use isnumeric() Python built-in function to be able to find all integers contained in the minstring ='1966grundade456for789data'. Then your program should add up the integers 1966456789 (1+9+6+6+4+5+6+7+8+9=61)

Tips: for loop, if vilkor, omvandling, från string to int. summa += int(min string)

Reference: https://docs.python.org/3/library/stdtypes.html


This is the final code. The documentation of the code is written below.

""" Use isnumeric() Python built-in function to be able to find all integers 
contained in the minstring ='1966grundade456for789data'. Then your program 
should add up the integers  1966456789 (1+9+6+6+4+5+6+7+8+9=61)
 """

minstring = "1966grundade456for789data"   

# empty variable container for the integers from minstring
integers = " "

# variable for the integers sum
sum_of_integers = 0

# Use isnumeric() to check if there are numbers and alphabet in the variable
if minstring.isnumeric():
    print(f'{minstring} is numeric')
else:
    print(f'{minstring} is not numeric')
# Output: 1966grundade456for789data is not numeric


# For loop iteration, i = variable, iterator = minstring
for i in minstring:
    # Use if statement. Use isnumeric function in "i"
    if i.isnumeric():
        # add numeric characters found in "i" into the "integers" variable
        integers += i

        # typecast from strings to integers
        sum_of_integers +=  int(i)

print(f'Found these integers from minstring: {integers}')
print(f'{sum_of_integers}')


s = " "
if len(s) == 0:
    print("empty string")
else:
    print("not empty string")

Understand the code first:

To iterate through each character in minstring repeatedly using a for loop.

Inside the for loop, the isnumeric() built-in function is used to take all numeric characters. The numeric characters will be placed in a new variable by increment and add the sum of all those integers.

for-loop

Let’s say I want to REPEAT print(“Run a code”). This is an ugly code to do it. Use for loop to make one line of code to perform and yield the same result.

Use the syntax for variable in the range function:

Range is a built-in function that tells no. of times to repeat the loop. I will use the for-loop to iterate through each character in the minstring variable later.

Output


isnumeric()

isnumeric() is a built-in function that returns all characters in a string that are numeric characters. The example, below, uses the isnumeric() and check if the minstring variable contains numeric characters. Now we know that minstring has both numeric and alphabet characters in it.


Create a variable named “integers”.

Create an empty variable integer. The isnumeric() function identifies the numeric characters in minstring, and stores it in the integers variable.

Create for loop

Create the for loop with syntax for variable in range:

“i” is a variable that represents each individual character during each iteration in the minstring range. For loop is used, not while loop, because the minstring range is already known. The number of repeated iterations are also known.

Use if statement

Inside the for loop, the function is.numeric(): is executed. The isnumeric() function iterates repeatedly through each character stored in the minstring.

Use the shorthand code +=

To take the numeric characters identified by is.numeric() to integers variable taken from i.

Output

Successfully identified, iterated and took the numeric characters.


Typecast the integers variable from strings to integers datatype

The map() method that I tried to convert strings to integers in a list but it didn’t work.

Output

Change to:

Create another new variable named sum_of_integers. This is the new container for the converted strings to integers datatype. Since the += shorthand code will be used, we can start counting from 0.

This code did not work.

Delete the “integers” variable because it is not the new container. Convert the int(i) variable into integers datatype here. Then, directly increment the results to sum_of_integers.


Final Code.

Output