home *** CD-ROM | disk | FTP | other *** search
- <TITLE>Sequence Types -- Python library reference</TITLE>
- Next: <A HREF="../m/mapping_types" TYPE="Next">Mapping Types</A>
- Prev: <A HREF="../n/numeric_types" TYPE="Prev">Numeric Types</A>
- Up: <A HREF="../t/types" TYPE="Up">Types</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H2>2.1.5. Sequence Types</H2>
- There are three sequence types: strings, lists and tuples.
- <P>
- Strings literals are written in single or double quotes:
- <CODE>'xyzzy'</CODE>, <CODE>"frobozz"</CODE>. See Chapter 2 of the Python
- Reference Manual for more about string literals. Lists are
- constructed with square brackets, separating items with commas:
- <CODE>[a, b, c]</CODE>. Tuples are constructed by the comma operator (not
- within square brackets), with or without enclosing parentheses, but an
- empty tuple must have the enclosing parentheses, e.g.,
- <CODE>a, b, c</CODE> or <CODE>()</CODE>. A single item tuple must have a trailing
- comma, e.g., <CODE>(d,)</CODE>.
- Sequence types support the following operations. The `<SAMP>in</SAMP>' and
- `<SAMP>not,in</SAMP>' operations have the same priorities as the comparison
- operations. The `<SAMP>+</SAMP>' and `<SAMP>*</SAMP>' operations have the same
- priority as the corresponding numeric operations.<A NAME="footnoteref1" HREF="#footnotetext1">(1)</A>
- <P>
- This table lists the sequence operations sorted in ascending priority
- (operations in the same box have the same priority). In the table,
- <VAR>s</VAR> and <VAR>t</VAR> are sequences of the same type; <VAR>n</VAR>, <VAR>i</VAR>
- and <VAR>j</VAR> are integers:
- <P>
- <DL>
- <DT><I>Operation</I><DD><I>Result</I> --- <I>Notes</I>
- <P>
- <DT><CODE><VAR>x</VAR> in <VAR>s</VAR></CODE><DD><CODE>1</CODE> if an item of <VAR>s</VAR> is equal to <VAR>x</VAR>, else <CODE>0</CODE>
- <DT><CODE><VAR>x</VAR> not in <VAR>s</VAR></CODE><DD><CODE>0</CODE> if an item of <VAR>s</VAR> is
- equal to <VAR>x</VAR>, else <CODE>1</CODE>
- <DT><CODE><VAR>s</VAR> + <VAR>t</VAR></CODE><DD>the concatenation of <VAR>s</VAR> and <VAR>t</VAR>
- <DT><CODE><VAR>s</VAR> * <VAR>n</VAR><R>,</R> <VAR>n</VAR> * <VAR>s</VAR></CODE><DD><VAR>n</VAR> copies of <VAR>s</VAR> concatenated
- <DT><CODE><VAR>s</VAR>[<VAR>i</VAR>]</CODE><DD><VAR>i</VAR>'th item of <VAR>s</VAR>, origin 0 --- (1)
- <DT><CODE><VAR>s</VAR>[<VAR>i</VAR>:<VAR>j</VAR>]</CODE><DD>slice of <VAR>s</VAR> from <VAR>i</VAR> to <VAR>j</VAR> --- (1), (2)
- <DT><CODE>len(<VAR>s</VAR>)</CODE><DD>length of <VAR>s</VAR>
- <DT><CODE>min(<VAR>s</VAR>)</CODE><DD>smallest item of <VAR>s</VAR>
- <DT><CODE>max(<VAR>s</VAR>)</CODE><DD>largest item of <VAR>s</VAR>
- </DL>
- Notes:
- <P>
- <DL>
- <DT><B>(1)</B><DD>If <VAR>i</VAR> or <VAR>j</VAR> is negative, the index is relative to
- the end of the string, i.e., <CODE>len(<VAR>s</VAR>) + <VAR>i</VAR></CODE> or
- <CODE>len(<VAR>s</VAR>) + <VAR>j</VAR></CODE> is substituted. But note that <CODE>-0</CODE> is
- still <CODE>0</CODE>.
- <P>
- <DT><B>(2)</B><DD>The slice of <VAR>s</VAR> from <VAR>i</VAR> to <VAR>j</VAR> is defined as
- the sequence of items with index <VAR>k</VAR> such that <CODE><VAR>i</VAR> <=
- <VAR>k</VAR> < <VAR>j</VAR></CODE>. If <VAR>i</VAR> or <VAR>j</VAR> is greater than
- <CODE>len(<VAR>s</VAR>)</CODE>, use <CODE>len(<VAR>s</VAR>)</CODE>. If <VAR>i</VAR> is omitted,
- use <CODE>0</CODE>. If <VAR>j</VAR> is omitted, use <CODE>len(<VAR>s</VAR>)</CODE>. If
- <VAR>i</VAR> is greater than or equal to <VAR>j</VAR>, the slice is empty.
- <P>
- </DL>
- <H2>Menu</H2><DL COMPACT>
- <DT><A HREF="../m/more_string_operations" TYPE=Menu>More String Operations</A>
- <DD><DT><A HREF="../m/mutable_sequence_types" TYPE=Menu>Mutable Sequence Types</A>
- <DD></DL>
- <H2>---------- Footnotes ----------</H2>
- <A NAME="footnotetext1" HREF="#footnoteref1">(1)</A>
- They must
- have since the parser can't tell the type of the operands.<P>
-