1. Home
  2. Computing & Technology
  3. Python

Sequence Types

by Al Lukaszewski
for About.com

Python recognises six sequence types: strings, Unicode strings, lists, tuples, buffers, and xrange objects.

String literals are written in single or double quotes. Unicode strings are much like strings, but are specified in the syntax using a preceding "u" character (e.g., u'cat', u"dog"). Lists are constructed with square brackets, separating items with commas. Tuples are constructed by the comma operator (without square brackets), with or without enclosing parentheses, but an empty tuple must have the enclosing parentheses, such as a, b, c or (). A single item tuple must have a trailing comma (e.g., '(d,)').

Buffer objects are not directly supported by Python syntax, but can be created by calling the builtin function buffer(). Concatenation and repetition are not supported, however.

Xrange objects are created using the xrange() function. They do not support slicing, concatenation or repetition. Using in, not in, min() or max() on them is inefficient.

Most sequence types support the following operations. The in and not in operations have the same priorities as the comparison operations. The + and * operators have the same priority as the corresponding numeric operations.

Below are the sequence operations sorted in ascending priority (s and t are sequences of the same type; n, y and j are integers).

  • x in s: True if an item of s is equal to x, else False. When s is a string (of either plain or Unicode format), the in and not in operations act like a substring test.
  • x not in s: False if an item of s is equal to x, else True. When s is a string (of either plain or Unicode format), the in and not in operations act like a substring test.
  • s + t: the concatenation of s and t. This type of concatenation is implementation dependent. A more reliable and consistent means for joining strings is to use str.join(). (6)
  • s * n , n * s: n shallow copies of s concatenated. Values of n that are less than zero are treated as zero.
  • s[y]: y'th item of s, origin 0. If i or j is negative, the index is oriented to the end of the string.
  • s[y:j]: slice of s from y to j. If i or j is negative, the index is oriented to the end of the string.
  • s[y:j:k]: slice of s from y to j with step k. If i or j is negative, the index is oriented to the end of the string.
  • len(s): length of s
  • min(s): smallest item of s
  • max(s): largest item of s

Explore Python
About.com Special Features

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

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Python
  4. Python Library
  5. Python Modules - Built-In Functions - Sequence Types>

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

All rights reserved.