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

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:

主站蜘蛛池模板: 深水埗区| 滦平县| 永和县| 龙游县| 灵石县| 富蕴县| 淅川县| 铜鼓县| 锦州市| 九寨沟县| 昌都县| 甘孜县| 白山市| 连州市| 娄烦县| 兴仁县| 安阳市| 砀山县| 独山县| 大兴区| 菏泽市| 万山特区| 遂昌县| 定安县| 肃北| 马鞍山市| 五指山市| 兴海县| 东乌珠穆沁旗| 思茅市| 峨山| 顺义区| 舞阳县| 荣昌县| 开江县| 翁源县| 阜南县| 建德市| 华安县| 即墨市| 三台县|