Section 6.2. Strings


6.2. Strings

Strings are among the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes. This contrasts with most other shell-type scripting languages, which use single quotes for literal strings and double quotes to allow escaping of characters. Python uses the "raw string" operator to create literal quotes, so no differentiation is necessary. Other languages such as C use single quotes for characters and double quotes for strings. Python does not have a character type; this is probably another reason why single and double quotes are treated the same.

Nearly every Python application uses strings in one form or another. Strings are a literal or scalar type, meaning they are treated by the interpreter as a singular value and are not containers that hold other Python objects. Strings are immutable, meaning that changing an element of a string requires creating a new string. Strings are made up of individual characters, and such elements of strings may be accessed sequentially via slicing.

With the unification of types and classes in 2.2, there are now actually three types of strings in Python. Both regular string (str) and Unicode string (unicode) types are actually subclassed from an abstract class called basestring. This class cannot be instantiated, and if you try to use the factory function to make one, you get this:

>>> basestring('foo') Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: The basestring type cannot be instantiated


How to Create and Assign Strings

Creating strings is as simple as using a scalar value or having the str() factory function make one and assigning it to a variable:

>>> aString = 'Hello World!'    # using single quotes >>> anotherString = "Python is cool!" # double quotes >>> print aString               # print, no quotes! Hello World! >>> anotherString               # no print, quotes! 'Python is cool!' >>> s = str(range(4))           # turn list to string >>> s '[0, 1, 2, 3]'


How to Access Values (Characters and Substrings) in Strings

Python does not support a character type; these are treated as strings of length one, thus also considered a substring. To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring:

>>> aString = 'Hello World!' >>> aString[0] 'H' >>> aString[1:5] 'ello' >>> aString[6:] 'World!'


How to Update Strings

You can "update" an existing string by (re)assigning a variable to another string. The new value can be related to its previous value or to a completely different string altogether.

>>> aString = aString[:6] + 'Python!' >>> aString 'Hello Python!' >>> aString = 'different string altogether' >>> aString 'different string altogether'


Like numbers, strings are not mutable, so you cannot change an existing string without creating a new one from scratch. That means that you cannot update individual characters or substrings in a string. However, as you can see above, there is nothing wrong with piecing together parts of your old string into a new string.

How to Remove Characters and Strings

To repeat what we just said, strings are immutable, so you cannot remove individual characters from an existing string. What you can do, however, is to empty the string, or to put together another string that drops the pieces you were not interested in.

Let us say you want to remove one letter from "Hello World!"...the (lowercase) letter "l," for example:

>>> aString = 'Hello World!' >>> aString = aString[:3] + aString[4:] >>> aString 'Helo World!'


To clear or remove a string, you assign an empty string or use the del statement, respectively:

>>> aString = '' >>> aString '' >>> del aString


In most applications, strings do not need to be explicitly deleted. Rather, the code defining the string eventually terminates, and the string is eventually deallocated.



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net