Computer Science, asked by radhikakhanna405, 3 months ago

Explain String concatenation function with example and syntax​

Answers

Answered by Equestriadash
11

String Concatenation is simply the adding/jσining of two or more strings together without a space.

It is done using the '+' operator.

Its syntax is as follows:

  • string1 + string 2 + ... string n

Let's use an example:

>>> print("Hello" + "World")

HelloWorld                            #output

>>> str1 = "Hello"

>>> str2 = "World"

>>> print(str1 + str2)

HelloWorld                           #output

Keep in mind, that you cannot add a string to an integer value.

For example:

>>> print("Hello" + 7)

Traceback (most recent call last):

 File "<pyshell#53>", line 1, in <module>

   print("Hello" + 7)

TypeError: can only concatenate str (not "int") to str

As seen, it returns an error saying that strings can only be added/concatenated to other strings.

>>> print("Hello" + "7")

Hello7                           #output; this is fine, since 7 is a string

Similar questions