Strings in programming are simply text; either individual characters, words, phrases, or complete sentences. They are one of the most common elements to use when programming, at least when it comes to interacting with the user. Because they are so common, they are a native data type within Python, meaning they have many powerful capabilities built in. Unlike other languages, you don't have to worry about creating these capabilities yourself.
Strings in Python are different than in most other languages. First off all, there are no char types, only single character strings (char types are single characters, separate from actual strings, used for memory conservation). Strings also can't be changed in-place; a new string object is created whenever you want to make changes to it, such as concatenation. This means you have to be aware that you are not manipulating the string in memory; it doesn't get changed or deleted as you work with it. You are simply creating a new string each time.
Empty strings are written as two quotes with nothing in between. The quotes used can be either single or double; my preference is to use double quotes, since you don't have to escape the single quote to use it in a string. That means you can write a statement like the following:
"And then he said, 'No way', when I told him."
If you want to use just one type of quote mark all the time, you have to use the backslash character to escape the desired quote marks so Python doesn't think it's at the end of the phrase, like this:
"And then he said, \"No way\", when I told him."
Triple quoted blocks are for strings that span multiple lines. Python collects the entire text block into a single string with embedded newline characters. This is good for things like writing short paragraphs of text; for example, instructions, or for formatting your source code for clarification.
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:
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:
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.
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:
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.
In the next post we'll cover Indexing and slicing strings.
0 comments:
Post a Comment