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

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:

主站蜘蛛池模板: 交口县| 枞阳县| 固原市| 九寨沟县| 南华县| 柳州市| 宜宾市| 东平县| 巴林右旗| 鸡泽县| 平江县| 珲春市| 绥棱县| 阿合奇县| 甘孜| 郓城县| 邯郸县| 娱乐| 泰和县| 桐梓县| 乐都县| 宿松县| 济南市| 德庆县| 越西县| 黎平县| 疏附县| 安顺市| 雅安市| 大渡口区| 游戏| 连云港市| 南京市| 信丰县| 来凤县| 攀枝花市| 册亨县| 芦溪县| 巩留县| 乐山市| 枣庄市|