1. Home
  2. Computing & Technology
  3. Python

Python Regular Expressions (regex): Forming a Regular Expression in Python

From , former About.com Guide

2 of 10

How Python Views a String: Every Dog has Its Null Value

The string 'dog' as contained in RAM.

The string 'dog' as contained in RAM.

A Lukaszewski

As you might expect, the computer reads strings the same way you do: one letter at a time. What is more, it recognizes character strings the same you do: by reading until it matches the word and then moving to the next. However, there are parts of character strings which the computer reads differently and some which the computer alone sees. These are important to keep in mind as you form a regular expression.

As an example of strings read differently, you and I read "\n" as a backslash followed by the letter en. But the computer will read "\n" (without the quotes) as an indicator of a new line. Similarly, you and I read " " as five blank spaces or possibly a tab space. For the computer, however, the latter option is represented by "\t", which we read as a backslash followed by a tee.

Similarly, character strings have parts which only the computer can see and distinguish. In memory (RAM), every string ends with what is called a null value ( ˆ… ). This is how the computer knows where the string begins and ends. (Note: how data is stored on a hard disk is determined largely by the file system in which the disk is formatted -- e.g., FAT32, NTFS, etc.). So, the string 'dog' actually looks like this image in memory ("|" represents the boundary between each block).

When the computer gets to ˆ…, it knows to go no further. In addition to this, there are incremental boundaries to string values that you should keep in mind.

Explore Python
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Python
  4. Regular Expressions
  5. Python Regular Expressions - Python Regex - How Python Views a String

©2009 About.com, a part of The New York Times Company.

All rights reserved.