← Back to challenges

Add Two String Numbers

PythonHardstringsnumberslanguage_fundamentalsmath

Instructions

Write a function that adds two numbers. The catch, however, is that the numbers will be strings.

Examples

add_str_nums("4", "5") ➞ "9"

add_str_nums("abcdefg", "3") ➞ "-1"

add_str_nums("1", "") ➞ "1"

add_str_nums("1874682736267235927359283579235789257", "32652983572985729") ➞ "1874682736267235927391936562808774986"

Notes

  • If there are any non-numerical characters, return "-1".
  • An empty parameter should be treated as "0".
  • Python supports the addition of integers without limit, try this challenge without using this functionality.
  • Your function doesn't have to add negative numbers.
  • Zeros at the beginning of the string should be trimmed.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.