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

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:

主站蜘蛛池模板: 信宜市| 当雄县| 四川省| 海宁市| 周宁县| 河源市| 建平县| 缙云县| 玉溪市| 且末县| 邛崃市| 福清市| 武城县| 东光县| 北流市| 漳平市| 伽师县| 泾阳县| 南涧| 密云县| 林甸县| 治县。| 凌海市| 清涧县| 比如县| 稷山县| 华亭县| 伊金霍洛旗| 垦利县| 临湘市| 天门市| 额敏县| 泽普县| 华池县| 外汇| 洛南县| 汾西县| 江陵县| 牙克石市| 双桥区| 奈曼旗|