Program to add two numbers represented as strings in Python

PythonServer Side ProgrammingProgramming

Suppose we have two strings S, and T, these two are representing an integer, we have to add them and find the result in the same string representation.

So, if the input is like "256478921657", "5871257468", then the output will be "262350179125", as 256478921657 + 5871257468 = 262350179125

To solve this, we will follow these steps −

  • convert S and T from string to integer
  • ret = S + T
  • return ret as string

Let us see the following implementation to get better understanding −

Example

 Live Demo

class Solution:
   def solve(self, a, b):
      return str(int(a) + int(b))
ob = Solution() print(ob.solve("256478921657", "5871257468"))

Input

"256478921657", "5871257468"

Output

262350179125
raja
Published on 05-Oct-2020 10:23:42
Advertisements