Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Internet & World Wide Web How to Program, 3/e
Internet & World Wide Web How to Program, 3/e

ISBN:
0-13-145091-3
© 2004
pages: 1420

Order
Amazon logo

This tutorial continues our introduction to Python with basic string input and output capabilities, and an introduction to regular expression processing with the Python re module. This tutorial is intended for students and developers who are already familiar with basic Python programming or who have read our prior Python tutorials (see the list at the bottom of this page).
Download the Code Examples
[Note: This tutorial is an excerpt (Section 35.4) of Chapter 35, Python, from our textbook Internet & World Wide Web How to Program, 3/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., INTERNET & WORLD WIDE WEB HOW TO PROGRAM, 3/E, 2004, pp.1254-1259. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
35.4   String Processing and Regular Expressions (Continued)
In line 12, three strings-string1, " " and string2-are concatenated with operator +. We then print this new string (string3).
Lines 16-17 create and print a string with a single character-an asterisk. Line 18 uses the *= statement to concatenate string4 to itself 10 times. We print the resulting string in line 19. Python also defines the += statement for strings, which effectively concatenates two strings. [Note: Since strings are immutable, the *= and += statements actually create new strings to perform their respective operations.]
Lines 22-26 illustrate the use of quotes in a string. Line 22 shows one method of displaying double quotes inside a string. The double quotes are displayed using the escape character (\). If we omit the escape character, then Python interprets the double-quote character as marking the end of the string, rather than as a character within the string itself. Line 23 presents another method of displaying double quotes inside a string. Note that the entire string is contained within single quotes ('). Python strings may be contained within either double quotes or single quotes. As line 23 demonstrates, if a string is contained within single quotes, then double quotes within the string do not need to be "escaped" with the backslash character. Similarly, if a string is contained within double quotes (line 25), then single quotes within the string do not need to be escaped.
If we do not want to escape quote characters in a string, we can place the entire string within pairs of three consecutive double-quote characters (line 26). This is called a triple-quoted string-triple-quoted strings may alternatively be surrounded by sets of three consecutive single-quote characters ('''). We use triple-quoted strings later in this chapter to output large blocks of XHTML from CGI scripts.
In lines 29-30, we use Python function raw_input to input the user's name and age. In line 31, we format a string to incorporate the input data. The % format character acts as a placeholder in the string. The format character s indicates that we want to place another string within the current string at the specified point. Figure 35.11 lists several format characters for use in string formatting. [Note: See Appendix E on number systems for a discussion of the numeric terminology in Fig. 35.11.]
Fig. 35.11 String-format characters.
Symbol
Meaning
c
Single character (i.e., a string of length 1).
s
String.
d
Signed decimal integer.
u
Unsigned decimal integer.
o
Unsigned octal integer.
x
Unsigned hexadecimal integer (using format abcdef).
X
Unsigned hexadecimal integer (using format ABCDEF).
f
Floating-point number.
e, E
Floating-point number (using scientific notation).
g, G
Floating-point number (using least-significant digits).
At the end of line 31, we use the % operator to indicate that the formatting characters in the string are to be replaced with the values listed between the parentheses. Python constructs the string from left to right by matching a placeholder with the next value specified between parentheses and replacing the formatting character with that value.
Pages in this Tutorial:   1 | 2 | 3 | 4
 
Additional Python Tutorials:
Introduction to Python

Python Basic Data Types, Control Statements and Functions

Tuples, Lists and Dictionaries

Python CGI Programming

Return to Tutorial Index