官术网_书友最值得收藏!

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:

主站蜘蛛池模板: 永和县| 淄博市| 昭平县| 河南省| 大同市| 岑巩县| 西平县| 靖远县| 长寿区| 观塘区| 兴国县| 高要市| 江门市| 湘西| 广安市| 石首市| 离岛区| 梅河口市| 礼泉县| 五大连池市| 永登县| 大兴区| 安溪县| 拜城县| 句容市| 镇江市| 扎赉特旗| 崇阳县| 当阳市| 济源市| 深圳市| 潞西市| 奎屯市| 合山市| 蚌埠市| 淮滨县| 铅山县| 建湖县| 开鲁县| 临汾市| 海原县|