Python Exercises: Calculate the sum of two numbers given as strings
Add numbers given as strings.
Write a Python program to calculate the sum of two numbers given as strings. Return the result in the same string representation.
Sample Data:
( “234242342341”, “2432342342”) -> “236674684683”
( “”, “2432342342”) -> False
( “1000”, “10”) -> “1010”
Sample Solution-1:
Python Code:
Sample Output:
Original string numbers: 234242342341 2432342342 Check said two strings contain three letters at the same index: 236674684683 Original string numbers: 2432342342 Check said two strings contain three letters at the same index: False Original string numbers: 1000 0 Check said two strings contain three letters at the same index: 1000 Original string numbers: 1000 10 Check said two strings contain three letters at the same index: 1010
Flowchart:
Sample Solution-2:
Python Code:
Sample Output:
Original string numbers: 234242342341 2432342342 Calculate the sum of two said numbers given as strings: 236674684683 Original string numbers: 2432342342 Calculate the sum of two said numbers given as strings: False Original string numbers: 1000 0 Calculate the sum of two said numbers given as strings: 1000 Original string numbers: 1000 10 Calculate the sum of two said numbers given as strings: 1010
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Python program to add two numbers provided as strings and return the result as a string without converting to integers using iterative digit addition.
- Write a Python program to convert two numeric strings to integers, sum them, and convert the result back to a string.
- Write a Python program to implement error checking when adding two strings as numbers, returning a specific message if conversion fails.
- Write a Python program to use recursion to add two numbers given as strings digit by digit and return the sum as a string.
Go to:
Previous Python Exercise: Alphabet position in a string.
Next Python Exercise: Sort a string based on its first character.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.