Practical Programming, 2nd Edition

Extracted from:

Practical Programming, 2nd Edition

An Introduction to Computer Science Using Python 3

This PDF file contains pages extracted from Practical Programming, 2nd Edition, published by the Pragmatic Bookshelf. For more information or to purchase a paperback or PDF copy, please visit .

Note: This extract contains some colored text (particularly in code listing). This is available only in online versions of the books. The printed versions are black and white. Pagination might vary between the online and printed versions; the

content is otherwise identical. Copyright ? 2013 The Pragmatic Programmers, LLC.

All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise,

without the prior consent of the publisher.

The Pragmatic Bookshelf

Dallas, Texas ? Raleigh, North Carolina

Practical Programming, 2nd Edition

An Introduction to Computer Science Using Python 3

Paul Gries Jennifer Campbell

Jason Montojo

The Pragmatic Bookshelf

Dallas, Texas ? Raleigh, North Carolina

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trademarks of The Pragmatic Programmers, LLC.

Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.

Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at .

The team that produced this book includes:

Lynn Beighley (editor) Potomac Indexing, LLC (indexer) Molly McBeath (copyeditor) David J Kelly (typesetter) Janet Furlow (producer) Juliet Benda (rights) Ellie Callahan (support)

Copyright ? 2013 The Pragmatic Programmers, LLC.

All rights reserved.

No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher.

Printed in the United States of America. ISBN-13: 978-1-93778-545-1 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0--September 2013

From email readers and web browsers to calendars and games, text plays a central role in computer programs. This chapter introduces a non-numeric data type that represents text, such as the words in this sentence or the sequence of bases in a strand of DNA. Along the way, we will see how to make programs a little more interactive by printing messages to our programs' users and getting input from them.

4.1 Creating Strings of Characters

Computers may have been invented to do arithmetic, but these days, most of them spend a lot of their time processing text. Many programs create text, store it, search it, and move it from one place to another.

In Python, text is represented as a string, which is a sequence of characters (letters, digits, and symbols). The type whose values are sequences of characters is str. The characters consist of those from the Latin alphabet found on most North American keyboards, as well as Chinese morphograms, chemical symbols, musical symbols, and much more.

In Python, we indicate that a value is a string by putting either single or double quotes around it. As we will see in Section 4.2, Using Special Characters in Strings, on page 8, single and double quotes are equivalent except for strings that contain quotes. You can use whichever you prefer. (For docstrings, the Python style guidelines say that double quotes are preferred.)

Here are two examples:

>>> 'Aristotle' 'Aristotle' >>> "Isaac Newton" 'Isaac Newton'

The opening and closing quotes must match:

>>> 'Charles Darwin" File "", line 1 'Charles Darwin" ^

SyntaxError: EOL while scanning string literal

EOL stands for "end of line." The error above indicates that the end of the line was reached before the end of the string (which should be marked with a closing single quote) was found.

Strings can contain any number of characters, limited only by computer memory. The shortest string is the empty string, containing no characters at all:

>>> ''

? Click HERE to purchase this book now. discuss

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download