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

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:

主站蜘蛛池模板: 临泉县| 宜州市| 修文县| 安溪县| 靖远县| 石河子市| 昌都县| 拜泉县| 普定县| 永定县| 乳山市| 安义县| 洛阳市| 芮城县| 襄垣县| 永川市| 新闻| 建德市| 东明县| 陇南市| 磐石市| 五峰| 濉溪县| 南召县| 柳州市| 常熟市| 永修县| 轮台县| 米脂县| 高密市| 裕民县| 苏尼特左旗| 杨浦区| 库尔勒市| 宜春市| 治多县| 汉沽区| 兴文县| 石首市| 石棉县| 麻江县|