- By Rob Miles
Working with text
In the previous chapter, we saw that we can write expressions that work with text. It turns out that we can also create variables that can hold text.
customer_name = 'Fred'
This statement looks exactly like the statement we used to create the total variable except that the value being assigned is a string of text. The variable customer_name is different from the total variable in that it holds text rather than a number. We can use this variable anywhere we would use a string.
message = 'the name is '+customer_name
In the expression being assigned, the text in the variable customer_name is added onto the end of the string "the name is ". As customer_name currently holds the string "Fred" (we set this in the previous statement), the above assignment would create another string variable called message which contains "the name is Fred".
Marking the start and end of strings
When I first saw how strings worked in Python, I wondered how to enter text containing a single quote. For example, let’s say you want to print the message, “It’s a trap.” We know Python uses the single quote character to define the limits (or delimit) of a string of text. However, the single quote in the word “it’s” would confuse Python, making it think the string had ended early.
One way to solve this problem is to enclose the string with double quotation marks rather than single quotation marks.
print("It's a trap")
Python lets you use either kind of quotation mark (single or double) to delimit a string of text in a program. This works, but of course the next thing I want to ask is, “How do you enter text that contains both single and double quotes?” The designer of Python thought of that, too, and allows us to use “triple quotes” to delimit a string. A triple quote is three single- or double-quote characters in a row:
print('''...and then Luke said "It's a trap"''')
This statement prints this message:
...and then Luke said "It's a trap"
Triple quoted strings look a bit cumbersome, but they have another advantage over “ordinary” strings. Any new lines in a triple-quoted string are made part of the string. To see how this might be useful, consider the instructions for the “Nerves of Steel” party game that we looked at in Chapter 3.
print('Welcome to Nerves of Steel') print() print('Everybody stand up') print('Stay standing as long as you dare.') print('Sit down just before you think the time will end.')
To produce these instructions, I had to write several print statements. By using a triple-quoted string, I can make this a lot easier.
print('''Welcome to Nerves of Steel Everybody stand up Stay standing as long as you dare. Sit down just before you think the time will end. ''')
The print statement now spans several lines. When the program runs, the new lines are printed so that the text looks as it did before, although it now spans several lines. Note that the blank line below the heading is also preserved when the print is performed.
One thing to remember is that you must use matching delimiters to start and end a string. If you start the string with triple quotes, you must end it that way, too.
Escape characters in text
Another way to include quote characters in a string of text is to use an escape sequence. Normally, each character in a string represents that character. In other words, an A in a string means ‘A’. However, when Python sees the escape character—the backslash (\) character—it looks at the text following the escape character to decide what character is being described. This is called an escape sequence. There are many different escape sequences you can use in a Python string. The most useful escape sequences are shown in the following table.
|ESCAPE SEQUENCE||WHAT IT MEANS||WHAT IT DOES|
|\\||Backslash character (\)||Enter a backslash into the string|
|\’||Single quote (‘)||Enter a single quote into the string|
|\”||Double quote (“)||Enter a double quote into the string|
|\n||ASCII Line Feed/New Line||End this line and take a new one|
|\t||ASCII Tab||Move to the right to the next tab stop|
|\r||ASCII Carriage return||Return the printing position to the start of the line|
|\a||ASCII Bell||Sound the bell on the terminal|
Python includes other escape sequences, but these will suffice for now.
If you’re wondering what ASCII (American Standard Code for Information Interchange) means, it is a mapping of numbers to printed characters. It was developed in the early 1960s for use by computers and persists to this day. (In Chapter 2, we learned that ASCII is the standard that maps the letter W to the decimal value 84).
ASCII is a perfectly fine standard if you don’t want to print more than 100 or so different characters. However, because many modern languages use more than 100 characters, UNICODE has become the new standard. UNICODE allows for many more characters, emojis, and emoticons. Some Python escape sequences produce UNICODE characters from our programs. UNICODE characters are frequently used in Graphical User Interfaces (GUIs)..
Not all the escape sequences above work on all computers. Also, their functions are not always consistent from one computer to another. For example, the \a escape sequence, which means ASCII Bell, was intended to sound the bell on a mechanical computer terminal. However, if you print this sequence, there is no guarantee that the computer you’re using will make a sound. The paragraph return character, \r, which is supposed to send the print head of a computer terminal back to the start of the line, is not very useful and may not do anything on some computers.
The most useful escape sequences are those that you can use to print quotes and the backspace character. You can also use the new line character (\n) to make new lines in strings you print.
Within Python programs, the end of a line of text is always marked by a single new line character. The underlying operating system might work differently, for example the Windows operating system uses the sequence “\r\n” (a return followed by a new line feed) to mark the end of each line of text in a file. The Python system performs automatic translation of line endings to match the computer system being used, so our programs can always use a single new line character to mark the end of a line.
Read in text using the input function
Up until now, all our programs have worked with values stored in the Python code itself. These are called literal values because they are literally “just there” in the code. We use the input function to make a “complete” program that takes in data, does something with it, and then produces a result.
name = input()
This statement would pause the program and wait for the user to type in a string and press the Enter key. The string of text is stored in the variable called name. We can make the program display a prompt for the user by adding a string to the call for the input function.
name=input('Enter your name please: ')
The name variable will contain whatever string the user types in. If the user just presses the Enter key without typing anything, the name variable will contain an empty string.
You can also use the input statement to pause your program so that the user can read the results.
input('Press Enter to continue')
In the statement above, the result of the call to the input function is ignored.