+ 1
Why we use concatenation in python?
Why add two strings together instead of just type two words.
3 Answers
+ 7
string concatination is usefull when you do not know the value of the strings you add together
s = "5 + 6 = " + "11"
wouldn't be very usefull here. However, in most code, you won't know most variable. Here is an example:
x = int(input())
y = int(input())
result = x + y
s = str(x) + " + " + str(y) + " = " + str(result)
print(s)
Using string concatination, I was able to print out the complete math used without knowing any actual values.
And little shortcut for those concatinations using f strings:
s = f"{x} + {y} = {result}"
I hope this helps!
+ 1
Every thing has its uses . as long as you are familiar with advanced problem in python you will find their application.
+ 1
changeless = " joined to chat"
changing = ["John", "Jane", " Brad", "David", "Jose"]
for name in changing:
print(name+changeless)
#outputs:
#John joined to chat
#Jane joined to chat
#....
#Jose joined to chat.