0

use of symbols between single qoutes

print ('1'+'2'+'3') fetches me 123 as output but print ('1'-'2'-'3') gives me an error. can i know why ??

30th Mar 2020, 9:44 AM
Mani
Mani - avatar
8 Antworten
+ 4
Hi! Because you are trying to subtract a string from a string. Like this: "Bob" - "Martin"= ? please, take a closer look at the Python tutorial, which explains operations with strings
30th Mar 2020, 9:49 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 5
'str1' + 'str2' concats the two strings so that it gives 'str1str2' but the - operator is not defined for strings. if you want to operate with the numeric value of a string str, use int(str) instead.
30th Mar 2020, 9:50 AM
John Robotane
John Robotane - avatar
+ 1
because in a string operation, plus doesn't work as an arithmetic addition sign. it is a command built into the language in order to glue the two strings together. but how do you imagine a reverse operation? it doesn't exist.
30th Mar 2020, 9:58 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
30th Mar 2020, 10:03 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Ярослав Вернигора(Yaroslav Vernigora) I'm still in Mod 1. I think I will learn this on the way. Sorry, I was bit too curious
30th Mar 2020, 10:11 AM
Mani
Mani - avatar
0
John Robotane I'm new to coding . But what you said makes sense, but I couldn't catch up. Gimme some examples so I can understand.
30th Mar 2020, 9:54 AM
Mani
Mani - avatar
0
Ярослав Вернигора(Yaroslav Vernigora) Thanks for the answer Actually I'm new to coding so how "bob"+"Martin" works And "Bob"-"Martin" doesn't work
30th Mar 2020, 9:56 AM
Mani
Mani - avatar
0
You are welcome! you are absolutely right! you will soon find out for yourself. by learning the lessons step by step. but I have already opened the veil of this mystery to you... 😉
30th Mar 2020, 10:15 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar