Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Add Two Numbers Represented as Strings in Python



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
Updated on: 2020-10-05T06:56:03+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements