Skip to content

Sum of two strings in Python | Example code

  • by

In Python to sum up two strings, you have to convert the string value to an integer for calculating the addition of any other arithmetic operation.

Use int() function to convert a string to an integer.

Example Sum of two strings in Python

A simple example code calculates the sum of two given string numbers.

def sum_method(first, second):
    return int(first) + int(second)


a = "100"
b = "200"

print(sum_method(a, b))

Output:

Sum of two strings in Python

How do I sum two string inputs using Python?

Answer: Technically, you can’t perform a sum operation on a string input. However, you can join strings together to form a new string.

Simply store the two strings in a variable and combine them into a new variable.

string_1 = input("Enter first string: ")
string_2 = input("Enter second string: ")

res = string_1 + string_2
print(res)

Output:

Enter first string: Hello
Enter second string: Jo
HelloJo

Comment if you have any doubts or suggestions on this Python string topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *