Python Write a function to find the sum of two integral numbers in string format

Here, we are actualizing a program in python, where we will compose a capacity that will acknowledge two basic sorts of numbers in string arrangement and return a total of the numbers as a whole number.

Given two indispensable numbers in string group, we need to characterize a capacity that can get these numbers, convert into whole numbers and return the total as the whole number in Python.

Example:

    Input:
    num1 = "10"
    num2 = "20"

    Function calling:
    calculateSum(num1, num2)

    Output:
    Sum = 30

Rationale:

  • Info two numbers in string position (We are simply doling out the hard-coded values here) note that numbers ought to be vital sort.
  • Characterize a capacity, pass these qualities as parameters.
  • Unequivocally convert the qualities to number by utilizing int(variable/esteem).
  • Ascertain the total and return it.

Program:

# function to calculate and return the sum
# parameters:
# a, b - integral numbers in string format
# return: sum of the numbers in integer format

def calculateSum (a,b):
	s = int(a) + int(b)
	return s 

# Main code 
# take two integral numbers as strings
num1 = "10"
num2 = "20"

# calculate sum
sum = calculateSum (num1, num2)

# print sum
print "Sum = ", sum

Output:

    Sum =  30

Leave a Comment

error: Alert: Content is protected!!