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

Basic string operations

The + and * operators are overloaded in Python, letting you concatenate (join together) and repeat string objects, respectively. Overloading is just using the same operator to do multiple things, based on the situation where it's used; you'll also see the term polymorphism. For example, the + symbol can mean addition when two numbers are involved or, as in this case, combining strings.

Concatenation combines two (or more) strings into a new string object, whereas repeat simply repeats a given string a given number of times.

The following screenshot demonstrates some of the Python operators that are overloaded. Line 21 shows string concatenation: the combining of multiple strings into a new, single string. In this case, the + operator is overloaded to combine strings but, when used with numbers (line 22), the operator will provide the sum of the values. Note that in line 23, trying to use the + operator with a number and a string results in an error, as Python doesn't know how to process that command. The error indicates that trying to use the + operator to combine an integer number with a text string won't work, because Python doesn't know whether you want to add two numbers or concatenate two strings. Therefore, the error states that combining the two data types is unsupported.

Line 24 shows the * operator used with a string to return multiple copies of that string in a new, combined string. Line 25 shows normal mathematical use of the * operator, returning the product of two numbers:

Operator overloading

Because Python doesn't know how to combine a string with a number, you can explicitly tell Python that a number should be a string through the str() function, as shown in the following screenshot. This is similar to casting values in C/C++. It informs Python that the number is not an integer or floating-point number but is, in reality, a text representation of the number. Just remember that you can no longer perform mathematical functions with it; it's strictly text:

Converting a number to a string

Iteration in strings is a little different than in other languages. Rather than creating a loop to continually go through the string and print out each character, Python has a built-in type for iteration, utilizing a for loop. The for loops are explained fully in Chapter 3, Logic Control, in the section, Loops but here is a brief explanation: Python accepts a given sequence and then performs one or more actions to each value within the sequence.

The following screenshot provides a demonstration of this:

String iteration

Line 27 assigns a string variable. In line 28, Python is sequentially going through the myjob variable and printing each character that exists in the string. By default, the print() function assigns a newline character to the end of each item that is printed. In this case, since we want to print each character that is in the lumberjack string, each character will be printed on a separate line.

If you want to print the results on a single line, you'll have to do a little printing manipulation. As a function, print() has some additional parameters available; in this case, we can use the end keyword as a print() argument, as shown in line 29, where the end  parameter (with no value) has been added. This tells Python that there should be no ending character inserted after printing an individual character, resulting in everything being printed on a single line.

主站蜘蛛池模板: 且末县| 宁陵县| 黔西县| 延川县| 禄丰县| 汝南县| 宁武县| 广州市| 枣强县| 禄丰县| 宁强县| 九台市| 谢通门县| 宝丰县| 九龙坡区| 呼玛县| 台江县| 英吉沙县| 信宜市| 天祝| 星子县| 沾益县| 白水县| 临潭县| 巫溪县| 清新县| 吉隆县| 辰溪县| 忻城县| 白山市| 兴隆县| 邯郸市| 清河县| 云南省| 津市市| 浙江省| 正定县| 哈巴河县| 子长县| 泰兴市| 前郭尔|