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

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:

主站蜘蛛池模板: 涞水县| 余庆县| 中山市| 雅江县| 万荣县| 吉安市| 永宁县| 望江县| 腾冲县| 城固县| 太保市| 太仆寺旗| 新建县| 九江市| 淳安县| 晋城| 嘉义市| 高要市| 全南县| 沾益县| 洞头县| 黔西县| 布尔津县| 昌都县| 西乡县| 南澳县| 鄂伦春自治旗| 深州市| 泗水县| 渝中区| 视频| 苗栗市| 毕节市| 正蓝旗| 斗六市| 万源市| 义乌市| 大同县| 镇康县| 旺苍县| 丁青县|