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

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:

主站蜘蛛池模板: 绥德县| 阳东县| 潼南县| 阳高县| 黔南| 萨嘎县| 临沂市| 青浦区| 陈巴尔虎旗| 博白县| 津市市| 如皋市| 方山县| 盐边县| 积石山| 吴川市| 灵丘县| 信宜市| 讷河市| 苍溪县| 二连浩特市| 宜良县| 游戏| 溧水县| 云浮市| 河间市| 津市市| 雅江县| 阜城县| 吐鲁番市| 象山县| 远安县| 高要市| 什邡市| 清镇市| 江安县| 金阳县| 汽车| 平凉市| 青河县| 城市|