crus4

logo

What Arithmetic Operators Cannot Be Used With Strings in Python


Arithmetic Operators such as Subtraction (-), multiplication(*), division (/) and exponentiation (**)cannot be used with strings in Python. Instead these operators are only used to perform mathematical operations on numerical values (integers and floats).

You can only use the addition operator (+) to concatenate the strings. For Example:

str1 = "hello"
str2 = "world"
result = (str1 + str2)
print(result)

Output

helloworld

If you use any other operator other than addition in Python, you will get an error. For example the below code will produce an error.

str1 = "hello"
str2 = "world"
result = (str1 - str2)
print(result)

This Code will produce an error.


What Arithmetic Operators Cannot Be Used With Strings in Python

Leave a Reply

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