Concatenating strings

In the preceding example, we printed the user inputs in combination with another string. For example, we took the user input name and printed the sentence as My name is Sai. The process of appending one string to another is called concatenation.

In Python, strings can be concatenated by adding + between two strings:

    name = input("What is your name? ") 
print("My name is " + name)

It is possible to concatenate two strings, but it is not possible to concatenate an integer. Let's consider the following example:

    id = 5 
print("My id is " + id)

It would throw an error implying that integers and strings cannot be combined:

An exception

It is possible to convert an integer to string and concatenate it to another string:

    print("My id is " + str(id))

This would give the following result:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset