In Python, ‘1’ + ‘2’ is equal to ’12’. This is because both ‘1’ and ‘2’ are strings so the + operation adds one string to the end of the other. This is called string concatenation. If you used integers instead, the result would be 3. String concatenation in Python means adding two or more strings together. In this process, one string is added to the end of the other. To concatenate two or more strings, call the + operator on the strings. For instance: Output: The same applies when applying string concatenation to strings that represent numeric values. For example: Output: Now that you understand how string
concatenation works, let’s take a look at a common issue beginners usually face. If you have a program that asks a user for two numbers to sum them up, you may bump into a strange issue: Example run: The result is obviously supposed to be 3, but it is 12. So what is the matter here? This is because the user inputs are strings by default—not numbers as you may assume. To solve
this little problem, convert the strings into numbers. If you want the user to input integers, you can convert the strings to integers using the built-in Here is how to do it: Now the program works as expected. Today you learned what Python string concatenation means why the result of ‘1’+’2′ is equal to ’12’ in Python.What Is
String Concatenation in Python
s1 = "Hello "
s2 = "World"
words = s1 + s2
print[words]
Hello World
s1 = '1'
s2 = '2'
both = s1 + s2
print[both]
12
Why Is 1+2=12 When It Should be 3?
number1 = input["Enter a number: "]
number2 = input["Enter another numnber: "]
result = number1 + number2
print[f"The result is {result}"]
Enter a number: 1
Enter another numnber: 2
The result is 12
int[]
function.number1 = int[input["Enter a number: "]]
number2 = int[input["Enter another numnber: "]]
result = number1 + number2
print[f"The result is {result}"]
Enter a number: 1
Enter another numnber: 2
The result is 3
Conclusion
To recap, string concatenation adds one string to the end of another.
When you ask for user data in your program, the data is a string by default. But you can convert it to an integer using the int[] function.
Thanks for reading.
Happy coding!
Further Reading
Single Quotes vs Double Quotes in Python Strings
Python Classes 101
Print[1%2] Output: 1 Why shouldn't be 0 ??? Can someone explain
Hey Mohammad Esmail Qasimi. First of all you have to understand the meaning of the % symbol in Python. In mathematics, that symbol is known to be a percentage but in python it is not. It is actually an operator symbol called a modulo operator. Now what does this mean? A modulo operator symbol is one that retains the remainder in an operation. Suppose x%y, this will output the remainder of x divided by y. So in your question, 1%2 simply means that when 1 is divided by 2, what is the remainder. The remainder is 1 because normal division of 1/2 will retain a 0 but remainder of 1. Hope you are answered.
% is the modulo operator, it give the remainder of an integer division. How often does 2 go in one? 0 So what remains? 1
Quite simply, this operator is called modulo which returns the remainder of a division: For instance, 9 divided by 4 equals 2 but it remains 1. Here, 9 / 4 = 2 and 9 % 4 = 1. 👤 🍎🍎🍎🍎 👤 🍎 🍏 🍎 👤 🍎🍎 👤 ______________________ 👤 🍎🍎 🍎🍎 👤 🍏 👤 🍎🍎 🍎🍎 👤 Think of it as if I asked you what's REMAINDER after dividing APPLES by 4 people. That's the purpose of this division process, so there's no need to look at anything other than a GREEN apple. In your example: 1 divided by 2 gives 0 but it remains 1 [1 % 2 == 1] So the modulo operation can be calculated using this equation: =================== a % b = a - floor[a / b] * b =================== ▪️ floor[a / b] represents the number of times you can divide a by b. ▪️ floor[a / b] * b is the amount that was successfully shared entirely. ▪️ The total [a] minus what was shared equals the remainder of the division. Applied to your example, this gives: 1 % 2 = 1 - floor[1/2] * 2 = 1
% is the module operator, it always give the remainder of an division.
Quantum , the remainder is 1. Suppose we have a/b=c. a is the divident b is the divisor c is the quotient And now suppose we have x%y=z. x is the divident y is the divisor z is the remainder. I hope it all makes sense
Hi Mohammad Esmail Qasimi You put it so well... How often 2[divisor] goes into 1[dividend] is 0 [quotient]... 1//2 would return this... 1%2 on the other hand returns the remainder, ergo, 1-0...
Hi Mohammad! 1 % 2 is not 0 because 1 / 2 = 0.5 and 0.5 is not a whole number. If the result of any division isn‘t a whole number then there is a remainder, thats why you have a comma in 0.5. So I give you some examples of Modulo: 7 % 3 means how many 3‘s you have in 7 and what remains: So 7 = 2 * 3 + 1 and what comes after the [+] is the remainder. Therefore 1 is the remainder here. 3 % 4 means how many 4‘s do you have in 3 and what remains: 3 = 0 * 4 + 3 so 3 is the remainder. 1 % 2 means how many 2‘s you have in 1 and what remains: 1 = 0 * 2 + 1 so 1 is the remainder thats why 1 % 2 = 1.
Thanks Mohammad Esmail Qasimi for marking my answer as best, im glad i was of help to you.
Thanks HungryTradie , i think you have put it in the simplest form anyone should be able to understand.
Oh you are going to be stubborn. 😔 New example: You and a friend [= 2 people] want to go to the zoo. You have 1 zoo entry ticket. You try to share 1 between 2 but you can't do it without fractions [or decimals called float]. You each get 0 tickets, the remaining tickets are 1. 1%2=1.
You can remember it like this... a%b = a if a