Python program to count the sum of numbers in a string
Count the sum of numbers in a string
Numbers can be added, but strings follow the rule of concatenation a string of alphabets will be concatenated before or after one another but in case of integers, numbers are added to each other to form a new number. Strings can be of many types:-
In this python program, we will use alphanumeric strings to find the sum of all numbers in that string.
- Step 1:- Start.
- Step 2:- Take user input as alphanumeric string.
- Step 3:- Initialize an integer variable with zero value.
- Step 4:- Check for numbers available in string.
- Step 5:- If numbers are found add number to integer variable by typecasting it to int.
- Step 6:- Print sum of the numbers available in string.
- Step 7:- End.
Python program to find sum of all numbers available in a string
#take user input String = input('Enter the alphanumeric string :') #initialize integer variable sum1 = 0 for i in String: #check if values lies between range of numbers or not #according to ascii tale if ord(i) >= 48 and ord(i) <= 57: #convert it to integer and add sum1 = sum1 + int(i) print('Sum is :' + str(sum1))
Enter the alphanumeric string :Daya123Ben456
Sum is :21