12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

msg'\naaaaaaaaaaaaa\nbbb\'\'\'bbbbbbbbbb""bbbbbbb\'bbbb\ncccccccccccccc'<strong>Python</strong> also supports a “raw” string literal that turns off the backslash escape mechanism(they start with the letter r), as well as a Unicode string form that supportsinternationalization (they begin with the letter u and contain multibyte characters).Technically, Unicode string is a different data type than normal string, but it supportsall the same string operations. We’ll meet all these special string forms in laterchapters.Pattern MatchingOne point worth noting before we move on is that none of the string object’s methodssupport pattern-based text processing. Text pattern matching is an advancedtool outside this book’s scope, but readers with backgrounds in other scripting languagesmay be interested to know that to do pattern matching in <strong>Python</strong>, we importa module called re. This module has analogous calls for searching, splitting, andreplacement, but because we can use patterns to specify substrings, we can be muchmore general:>>> import re>>> match = re.match('Hello[ \t]*(.*)world', 'Hello <strong>Python</strong> world')>>> match.group(1)'<strong>Python</strong> 'This example searches for a substring that begins with the word “Hello,” followed byzero or more tabs or spaces, followed by arbitrary characters to be saved as amatched group, terminated by the word “world.” If such as substring is found, portionsof the substring matched by parts of the pattern enclosed in parentheses areavailable as groups. The following pattern, for example, picks out three groups separatedby slashes:>>> match = re.match('/(.*)/(.*)/(.*)', '/usr/home/lumberjack')>>> match.groups( )('usr', 'home', 'lumberjack')Pattern matching is a fairly advanced text-processing tool by itself, but there is alsosupport in <strong>Python</strong> for even more advanced language processing, including naturallanguage processing. I’ve already said enough about strings for this tutorial, though,so let’s move on to the next type.ListsThe <strong>Python</strong> list object is the most general sequence provided by the language. Listsare positionally ordered collections of arbitrarily typed objects, and they have nofixed size. They are also mutable—unlike strings, lists can be modified in-place byassignment to offsets as well as a variety of list method calls.Lists | 75

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!