Interacting With Strings In Python - Part I
Your computer
doesn’t understand strings. It’s a basic fact. Computers understand numbers,
not letters. When you see a string on the computer screen, the computer
actually sees a series of numbers. However, humans understand strings quite
well, so applications need to be able to work with them. Fortunately, Python
makes working with strings relatively easy. It translates the string you
understand into the numbers the computer understands, and vice versa.
In order to make
strings useful, you need to be able to manipulate them. Of course, that means
taking strings apart and using just the pieces you need or searching the string
for specific information. This post describes how you can build strings using
Python, dissect them as needed, and use just the parts you want after you find
what’s required. String manipulation is an important part of applications
because humans depend on computers per-forming that sort of work for them (even
though the computer has no idea of what a string is).
After you have the
string you want, you need to present it to the user in an eye-pleasing manner.
The computer doesn’t really care how it presents the string, so often you get
the information, but it lacks pizzazz. In fact, it may be downright difficult
to read. Knowing how to format strings so that they look nice onscreen is
important because users need to see information in a form they understand. By
the time you complete this post, you know how to create, manipulate, and format
strings so that the user sees precisely the right information.
Most aspiring
developers (and even a few who have written code for a long time) really have a
hard time understanding that computers truly do only understand 0s and 1s. Even
larger numbers are made up of 0s and 1s. Comparisons take place with 0s and 1s.
Data is moved using 0s and 1s. In short, strings don’t exist for the computer
(and numbers just barely exist). Although grouping 0s and 1s to make numbers is
relatively easy, strings are a lot harder because now you’re talking about
information that the computer must manipulate as numbers but present as
characters.
There are no
strings in computer science. Strings are made up of characters, and individual
characters are actually numeric values. When you work with strings in Python,
what you’re really doing is creating an assembly of charac-ters that the
computer sees as numeric values. That’s why the following sec-tions are so
important. They help you understand why strings are so special. Understanding
this material will save you a lot of headaches later.
Defining a
character using numbers
To create a
character, you must first define a relationship between that character and a
number. More important, everyone must agree that when a certain number appears
in an application and is viewed as a character by that application, the number
is translated into a specific character. One of the most common ways to perform
this task is to use the American Standard Code for Information Interchange
(ASCII). Python uses ASCII to translate the number 65 to the letter A. The
chart at http://www.asciitable.com/ shows the various numeric values and their
character equivalents.
Every character
you use must have a different numeric value assigned to it. The letter A uses a
value of 65. To create a lowercase a, you must assign a different number, which
is 97. The computer views A and a as completely different characters, even
though people view them as uppercase and lower-case versions of the same
character.
The numeric values
used in this post are in decimal. However, the com-puter still views them as 0s
and 1s. For example, the letter A is really the value 01000001 and the letter a
is really the value 01100001. When you see an A onscreen, the computer sees a
binary value instead.
Having just one
character set to deal with would be nice. However, not every-one could agree on
a single set of numeric values to equate with specific characters. Part of the
problem is that ASCII doesn’t support characters used by other languages; also,
it lacks the capability to translate special characters into an onscreen
presentation. In fact, character sets abound. You can see a number of them at
http://www.i18nguy.com/unicode/codepages. html. Click one of the character set
entries to see how it assigns specific numeric values to each character. Most
characters sets do use ASCII as a starting point.
Using
characters to create strings
Python doesn’t
make you jump through hoops to create strings. However, the term string should
actually give you a good idea of what happens. Think about beads or anything
else you might string. You place one bead at a time onto the string. Eventually
you end up with some type of ornamentation — perhaps a necklace or tree
garland. The point is that these items are made up of individual beads.
The same concept
used for necklaces made of beads holds true for strings in computers. When you
see a sentence, you understand that the sentence is made up of individual
characters that are strung together by the program-ming language you use. The
language creates a structure that holds the indi-vidual characters together.
So, the language, not the computer, knows that so many numbers in a row (each
number being represented as a character) defines a string such as a sentence.
You may wonder why
it’s important to even know how Python works with characters. The reason is
that many of the functions and special features that Python provides work with
individual characters, and it’s important to know that Python sees the
individual characters. Even though you see a sentence, Python sees a specific
number of characters.
Unlike most
programming languages, strings can use either single quotes or double quotes.
For example, “Hello There!” with double quotes is a string, as is ‘Hello
There!’ with single quotes. Python also supports triple double and single
quotes that let you create strings spanning multiple lines. The following steps
help you create an example that demonstrates some of the string features that
Python provides.
Open a Python File window.
You see an editor
in which you can type the example code.
Type the following code into the window —
pressing Enter after each line:
print('Hello There
(Single Quote)!') print("Hello There (Double Quote)!")
print("""This is a multiple line string using triple double
quotes.
You can also use
triple single quotes.""")
Each of the three
print() function calls demonstrates a different prin-ciple in working with
strings. It’s equally acceptable to enclose the string in either single or
double quotes. When you use a triple quote (either single or double), the text
can appear on multiple lines.
Choose Run➪Run Module.
You see a Python
Shell window open. The application outputs the text. Notice that the multiline
text appears on three lines, just as it does in the source code file, so this
is a kind of formatting. You can use multiline formatting to ensure that the
text breaks where you want it to onscreen.
0 comments:
Post a Comment