Interacting With Strings in Python - Part 2
Creating Stings
with Special Characters
Some strings
include special characters. These characters are different from the
alphanumeric and punctuation characters that you’re used to using. In fact,
they fall into these categories:
Control: An application requires some means of
determining that a par-ticular character isn’t meant to be displayed but rather
to control the display. All the control movements are based on the insertion
pointer, the line you see when you type text on the screen. For example, you
don’t see a tab character. The tab character provides a space between two
elements, and the size of that space is controlled by a tab stop. Likewise,
when you want to go to the next line, you use a carriage return (which returns
the insertion pointer to the beginning of the line) and linefeed (which places
the insertion pointer on the next line) combination.
Accented: Characters that have accents, such
as the acute (‘), grave (`), circumflex (^), umlaut or diaeresis (¨), tilde
(~), or ring ( ),̊represent special spoken sounds, in most cases. You must use
special characters to create alphabetical characters with these accents
included.
Drawing: It’s possible to create rudimentary
art with some characters. You can see examples of the box-drawing characters at
http://jrgraphix. net/r/Unicode/2500-257F. Some people actually create art
using ASCII characters as well (http://www.asciiworld.com/).
Typographical: A number of typographical
characters, such as the pilcrow (¶),are used when displaying certain kinds of
text onscreen, especially when the application acts as an editor.
Other: Depending on the character set you use,
the selection of characters is nearly endless. You can find a character for
just about any need. The point is that you need some means of telling Python
how to present these special characters.
A common need when
working with strings, even strings from simple console applications, is control
characters. With this in mind, Python provides escape sequences that you use to
define control characters directly (and a special escape sequence for other
characters).
An escape sequence
literally escapes the common meaning of a letter, such as a, and gives it a new
meaning (such as the ASCII bell or beep). The combination of the backslash (\)
and a letter (such as a) is commonly viewed as a single letter by developers —
an escape character or escape code.
Escape Meaning
Sequence
\newline Ignored
\\ Backslash (\)
\’ Single quote
(‘)
\" Double
quote (")
\a ASCII Bell
(BEL)
\b ASCII Backspace
(BS)
\f ASCII Formfeed
(FF)
\n ASCII Linefeed
(LF)
\r ASCII Carriage
Return (CR)
\t ASCII
Horizontal Tab (TAB)
\uhhhh Unicode
character (a specific kind of character set with
broad appeal across the world) with a
hexadecimal value that
replaces hhhh
\v ASCII Vertical
Tab (VT)
\ooo ASCII
character with octal numeric value that replaces ooo
\xhh ASCII
character with hexadecimal value that replaces hh
The best way to
see how the escape sequences work is to try them. The following steps help you
create an example that tests various escape sequences so that you can see them
in action.
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("Part
of this text\r\nis on the next line.") print("This is an A with a
grave accent: \xC0.") print("This is a drawing character:
\u2562.") print("This is a pilcrow: \266.")
print("This
is a division sign: \xF7.")
The example code
uses various techniques to achieve the same end — to create a special
character. Of course, you use control characters directly, as shown in the
first line. Many special letters are accessible using a hexadecimal number that
has two digits (as in the second and fifth lines). However, some require that
you rely on Unicode numbers (which always require four digits), as shown in the
third line. Octal values use three digits and have no special character
associated with them, as shown in the fourth line.
Choose Run➪Run Module.
You see a Python
Shell window open. The application outputs the expected text and special
characters.
The Python shell uses a standard character set across platforms,
so the Python Shell should use the same special characters no matter which
platform you test. However, when creating your application, make sure to test
it on various platforms to see how the application will react. A character set
on one platform may use different numbers for special characters than another
platform does. In addition, user selec-tion of character sets could have an
impact on how special characters displayed by your application appear. Always
make sure that you test special character usage completely.
Selecting
Individual Characters
Earlier in the post,
you discover that strings are made up of individual characters. They are, in
fact, just like beads on a necklace — with each bead being an individual
element of the whole string.
Python makes it
possible to access individual characters in a string. This is an important
feature because you can use it to create new strings that contain only part of
the original. In addition, you can combine strings to create new results. The
secret to this feature is the square bracket. You place a square bracket with a
number in it after the name of the variable. Here’s an example:
MyString =
"Hello World" print(MyString[0])
In this case, the
output of the code is the letter H. Python strings are zero-based, which means
they start with the number 0 and proceed from there. For example, if you were
to type print(MyString[1]), the output would be the letter e.
You can also
obtain a range of characters from a string. Simply provide the beginning and
ending letter count separated by a colon in the square brackets. For example,
print(MyString[6:11]) would output the word World. The output would begin with
letter 7 and end with letter 12 (remember that the index is zero based).
The following
steps demonstrate some basic tasks that you can perform using Python’s
character-selection technique.
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.
String1 =
"Hello World"
String2 =
"Python is Fun!"
print(String1[0])
print(String1[0:5])
print(String1[:5])
print(String1[6:])
String3 =
String1[:6] + String2[:6] print(String3)
print(String2[:7]*5)
The example begins
by creating two strings. It then demonstrates vari-ous methods for using the
index on the first string. Notice that you can leave out the beginning or
ending number in a range if you want to work with the remainder of that string.
The next step is
to combine two substrings. In this case, the code com-bines the beginning of
String1 with the beginning of String2 to create
String3.
The use of the +
sign to combine two strings is called concatenation. It’s one of the handier
operators to remember when you’re working with strings in an application.
The final step is
to use a Python feature called repetition. You use repetition to make a number
of copies of a string or substring.
Choose Run➪Run Module.
You see a Python
Shell window open. The applications outputs a series of substrings and string
combinations.
0 comments:
Post a Comment