+ 1
Can someone please explain to me "concatenate" esp the question of adding the name john and mary, please help me #newbie
3 ответов
+ 4
In which programming language are you talking about? And share the code also then we can clarify what's your doubt.
+ 4
Oh, you mean concatenate two strings together, returning a new long one?
In Python, concatenate strings together means "adding" them together using the "+" operator. It returns a new string created by 2 or more strings combined..
This is useful if you want to add a string to another string from a loop(We mostly use loops for this, but it's(concatenate strings) not useless without loops)..
NOTE: String is not a number, even if you add numbers on it. So, "12"+2 would return an error, 12+2 would return 14, BUT "12"+"2" would return 122(string type)..
- Concatenate strings example:
#Example1
string1 = "Hello"
string2 = " World!"
string3 = string1+string2
print(string3) #Hello World!
#Example2
name = "Mary"
print("My name is "+name) #My name is Mary
#Example3
stringNums = "123"
print(stringNums+"456") #123456
print(123+456) #579
#print(stringNums+456) #ERROR
You can call any types of objects(Including strings,integers,...) in the print() statement using the **comma**(,).
print("Hello","Mike") #Hello Mike
0
If you still don't understand, let's say:
You have a small piece of paper number 1 and you wrote "Hello" on it, and you have another small piece of paper number 2 and you wrote "World". Now, combine number 1 and 2 together, you get "Hello World".