Deitel & Associates, Inc. Logo

Back to
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

© 2004
pages: 1420

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
Programmers use string processing to accomplish a variety of tasks. System administration scripts can use Python modules and strings to process text files. Web programmers can use Python CGI scripts to compose and modify Web pages, to validate user-entered data from an XHTML form or to aggregate and display data from a variety of sources. This section discusses simple string processing in Python, including the use of regular expressions. A regular expression string defines a pattern with which text data can be compared. Regular expressions are used to search through strings, text files, databases, and so on. Regular expressions are not part of the core Python language, but regular expression processing capability is available through the standard Python re module.
Figure 35.10 demonstrates the use of strings in Python. Lines 5-6 assign the value "This is a string." to variable string1 and print that value to the screen. In lines 8-9, we assign a similar value to variable string2 and print that string.
Fig. 35.10 Using strings in Python.
1   # Fig. 35.10:
2   # Program to illustrate use of strings
4   # simple string assignments
5   string1 = "This is a string."
6   print string1
8   string2 = "This is a second string."
9   print string2
11   # string concatenation
12   string3 = string1 + " " + string2
13   print string3
15   # using operators
16   string4 = '*'
17   print "String with an asterisk: " + string4
18   string4 *= 10
19   print "String with 10 asterisks: " + string4
21   # using quotes
22   print "This is a string with \"double quotes.\""
23   print 'This is another string with "double quotes."'
24   print 'This is a string with \'single quotes.\''
25   print "This is another string with 'single quotes.'"
26   print """This string has "double quotes" and 'single quotes.'"""
28   # string formatting
29   name = raw_input( "Enter your name: " )
30   age = raw_input( "Enter your age: " )
31   print "Hello, %s, you are %s years old." % ( name, age )

This is a string.
This is a second string.
This is a string. This is a second string.
String with an asterisk: *
String with 10 asterisks: **********
This is a string with "double quotes."
This is another string with "double quotes."
This is a string with 'single quotes.'
This is another string with 'single quotes.'
This string has "double quotes" and 'single quotes.'
Enter your name: Brian
Enter your age: 33
Hello, Brian, you are 33 years old.
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