Why we can not subtract strings even if we can add? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why we can not subtract strings even if we can add?

Eg: print ('3' +'3') gives 33 print ('3' - '3') gives error

9th Jul 2021, 3:27 PM
Pranjal Rastogi
Pranjal Rastogi - avatar
7 Answers
+ 4
What do you expect from subtracting string? In your case do you expect an empty string? What about "334"-"3", "abc"-"&", "1244"-"4421"? Well, if there is a rule of subtracting string, it would hardly make sense. To remove a part of the string, you can use slice.
9th Jul 2021, 3:27 PM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 4
The class string did not implement it. https://code.sololearn.com/cJLVkXZN1Vlw/?ref=app
9th Jul 2021, 3:29 PM
Oma Falk
Oma Falk - avatar
+ 3
String subtraction makes no sense, but if you want results like these: "ABC" - "BC" = "A" "ABC" - "AB" = "C" then you can use str.replace method: "ABC".replace("BC", "") = "A" "ABC".replace("AB", "") = "B" str1.replace(str2, str3) gives a copy of string str1, where all occurences of substrings str2 will be replaced with substrings str3.
9th Jul 2021, 4:03 PM
Seb TheS
Seb TheS - avatar
+ 1
Because "+" is not only a mathematical operator for summing numbers, but also a concatenation operator that concatenates strings.
9th Jul 2021, 3:52 PM
Solo
Solo - avatar
+ 1
LoL! + operator on string doesn't add them It concatenate two strings. And - sign gives error because programmer of PYTHON didn't add that in syntax
10th Jul 2021, 5:52 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
0
That's not addition, it is concatenation...
9th Jul 2021, 4:30 PM
Divya Mohan
Divya Mohan - avatar
- 2
Print(name_input())
11th Jul 2021, 8:33 AM
Anbarasu .k
Anbarasu .k - avatar