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

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:

主站蜘蛛池模板: 昭平县| 江津市| 福贡县| 花莲县| 新丰县| 鄂州市| 太仓市| 盐池县| 长沙市| 乌拉特中旗| 沙田区| 昔阳县| 东乌| 宜丰县| 敖汉旗| 开原市| 通渭县| 浦城县| 从江县| 宁都县| 永平县| 晋城| 山东省| 宜城市| 三亚市| 红原县| 新乡市| 防城港市| 博乐市| 攀枝花市| 永宁县| 措美县| 晋州市| 绵阳市| 泰兴市| 宜宾市| 清徐县| 乐业县| 邯郸县| 大余县| 图木舒克市|