Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="types-set.html" /> | |
13 | <link rel="prev" href="typeiter.html" /> | |
14 | <link rel="parent" href="types.html" /> | |
15 | <link rel="next" href="string-methods.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>2.3.6 Sequence Types -- str, unicode, list, tuple, buffer, xrange </title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="2.3.5 Iterator Types" | |
25 | href="typeiter.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="2.3 Built-in Types" | |
28 | href="types.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="2.3.6.1 String Methods" | |
31 | href="string-methods.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="typeiter.html">2.3.5 Iterator Types</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="types.html">2.3 Built-in Types</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="string-methods.html">2.3.6.1 String Methods</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION004360000000000000000"></A><A NAME="typesseq"></A> | |
56 | <BR> | |
57 | 2.3.6 Sequence Types -- | |
58 | <tt class="class">str</tt>, <tt class="class">unicode</tt>, <tt class="class">list</tt>, | |
59 | <tt class="class">tuple</tt>, <tt class="class">buffer</tt>, <tt class="class">xrange</tt> | |
60 | ||
61 | </H2> | |
62 | ||
63 | <P> | |
64 | There are six sequence types: strings, Unicode strings, lists, | |
65 | tuples, buffers, and xrange objects. | |
66 | ||
67 | <P> | |
68 | String literals are written in single or double quotes: | |
69 | <code>'xyzzy'</code>, <code>"frobozz"</code>. See chapter 2 of the | |
70 | <em class="citetitle"><a | |
71 | href="../ref/strings.html" | |
72 | title="Python Reference Manual" | |
73 | >Python Reference Manual</a></em> for more about | |
74 | string literals. Unicode strings are much like strings, but are | |
75 | specified in the syntax using a preceding "<tt class="character">u</tt>" character: | |
76 | <code>u'abc'</code>, <code>u"def"</code>. Lists are constructed with square brackets, | |
77 | separating items with commas: <code>[a, b, c]</code>. Tuples are | |
78 | constructed by the comma operator (not within square brackets), with | |
79 | or without enclosing parentheses, but an empty tuple must have the | |
80 | enclosing parentheses, such as <code>a, b, c</code> or <code>()</code>. A single | |
81 | item tuple must have a trailing comma, such as <code>(d,)</code>. | |
82 | <a id='l2h-156' xml:id='l2h-156'></a><a id='l2h-157' xml:id='l2h-157'></a><a id='l2h-158' xml:id='l2h-158'></a><a id='l2h-159' xml:id='l2h-159'></a><a id='l2h-160' xml:id='l2h-160'></a> | |
83 | <P> | |
84 | Buffer objects are not directly supported by Python syntax, but can be | |
85 | created by calling the builtin function | |
86 | <tt class="function">buffer()</tt>.<a id='l2h-161' xml:id='l2h-161'></a> They don't support | |
87 | concatenation or repetition. | |
88 | <a id='l2h-162' xml:id='l2h-162'></a> | |
89 | <P> | |
90 | Xrange objects are similar to buffers in that there is no specific | |
91 | syntax to create them, but they are created using the <tt class="function">xrange()</tt> | |
92 | function.<a id='l2h-163' xml:id='l2h-163'></a> They don't support slicing, | |
93 | concatenation or repetition, and using <code>in</code>, <code>not in</code>, | |
94 | <tt class="function">min()</tt> or <tt class="function">max()</tt> on them is inefficient. | |
95 | <a id='l2h-164' xml:id='l2h-164'></a> | |
96 | <P> | |
97 | Most sequence types support the following operations. The "<tt class="samp">in</tt>" and | |
98 | "<tt class="samp">not in</tt>" operations have the same priorities as the comparison | |
99 | operations. The "<tt class="samp">+</tt>" and "<tt class="samp">*</tt>" operations have the same | |
100 | priority as the corresponding numeric operations.<A NAME="tex2html8" | |
101 | HREF="#foot1961"><SUP>2.7</SUP></A> | |
102 | <P> | |
103 | This table lists the sequence operations sorted in ascending priority | |
104 | (operations in the same box have the same priority). In the table, | |
105 | <var>s</var> and <var>t</var> are sequences of the same type; <var>n</var>, <var>i</var> | |
106 | and <var>j</var> are integers: | |
107 | ||
108 | <P> | |
109 | <div class="center"><table class="realtable"> | |
110 | <thead> | |
111 | <tr> | |
112 | <th class="center">Operation</th> | |
113 | <th class="left" >Result</th> | |
114 | <th class="center">Notes</th> | |
115 | </tr> | |
116 | </thead> | |
117 | <tbody> | |
118 | <tr><td class="center" valign="baseline"><code><var>x</var> in <var>s</var></code></td> | |
119 | <td class="left" ><code>True</code> if an item of <var>s</var> is equal to <var>x</var>, else <code>False</code></td> | |
120 | <td class="center">(1)</td></tr> | |
121 | <tr><td class="center" valign="baseline"><code><var>x</var> not in <var>s</var></code></td> | |
122 | <td class="left" ><code>False</code> if an item of <var>s</var> is | |
123 | equal to <var>x</var>, else <code>True</code></td> | |
124 | <td class="center">(1)</td></tr> | |
125 | ||
126 | <tr><td class="center" valign="baseline"><code><var>s</var> + <var>t</var></code></td> | |
127 | <td class="left" >the concatenation of <var>s</var> and <var>t</var></td> | |
128 | <td class="center">(6)</td></tr> | |
129 | <tr><td class="center" valign="baseline"><code><var>s</var> * <var>n</var> , <var>n</var> * <var>s</var></code></td> | |
130 | <td class="left" ><var>n</var> shallow copies of <var>s</var> concatenated</td> | |
131 | <td class="center">(2)</td></tr> | |
132 | ||
133 | <tr><td class="center" valign="baseline"><code><var>s</var>[<var>i</var>]</code></td> | |
134 | <td class="left" ><var>i</var>'th item of <var>s</var>, origin 0</td> | |
135 | <td class="center">(3)</td></tr> | |
136 | <tr><td class="center" valign="baseline"><code><var>s</var>[<var>i</var>:<var>j</var>]</code></td> | |
137 | <td class="left" >slice of <var>s</var> from <var>i</var> to <var>j</var></td> | |
138 | <td class="center">(3), (4)</td></tr> | |
139 | <tr><td class="center" valign="baseline"><code><var>s</var>[<var>i</var>:<var>j</var>:<var>k</var>]</code></td> | |
140 | <td class="left" >slice of <var>s</var> from <var>i</var> to <var>j</var> with step <var>k</var></td> | |
141 | <td class="center">(3), (5)</td></tr> | |
142 | ||
143 | <tr><td class="center" valign="baseline"><code>len(<var>s</var>)</code></td> | |
144 | <td class="left" >length of <var>s</var></td> | |
145 | <td class="center"></td></tr> | |
146 | <tr><td class="center" valign="baseline"><code>min(<var>s</var>)</code></td> | |
147 | <td class="left" >smallest item of <var>s</var></td> | |
148 | <td class="center"></td></tr> | |
149 | <tr><td class="center" valign="baseline"><code>max(<var>s</var>)</code></td> | |
150 | <td class="left" >largest item of <var>s</var></td> | |
151 | <td class="center"></td></tr></tbody> | |
152 | </table></div> | |
153 | <a id='l2h-165' xml:id='l2h-165'></a><a id='l2h-166' xml:id='l2h-166'></a><a id='l2h-167' xml:id='l2h-167'></a><a id='l2h-168' xml:id='l2h-168'></a><a id='l2h-169' xml:id='l2h-169'></a><a id='l2h-170' xml:id='l2h-170'></a><a id='l2h-171' xml:id='l2h-171'></a><a id='l2h-172' xml:id='l2h-172'></a><a id='l2h-173' xml:id='l2h-173'></a><a id='l2h-174' xml:id='l2h-174'></a><a id='l2h-175' xml:id='l2h-175'></a> | |
154 | <P> | |
155 | Notes: | |
156 | ||
157 | <P> | |
158 | <DL> | |
159 | <DT><STRONG>(1)</STRONG></DT> | |
160 | <DD>When <var>s</var> is a string or Unicode string object the | |
161 | <code>in</code> and <code>not in</code> operations act like a substring test. In | |
162 | Python versions before 2.3, <var>x</var> had to be a string of length 1. | |
163 | In Python 2.3 and beyond, <var>x</var> may be a string of any length. | |
164 | ||
165 | <P> | |
166 | </DD> | |
167 | <DT><STRONG>(2)</STRONG></DT> | |
168 | <DD>Values of <var>n</var> less than <code>0</code> are treated as | |
169 | <code>0</code> (which yields an empty sequence of the same type as | |
170 | <var>s</var>). Note also that the copies are shallow; nested structures | |
171 | are not copied. This often haunts new Python programmers; consider: | |
172 | ||
173 | <P> | |
174 | <div class="verbatim"><pre> | |
175 | >>> lists = [[]] * 3 | |
176 | >>> lists | |
177 | [[], [], []] | |
178 | >>> lists[0].append(3) | |
179 | >>> lists | |
180 | [[3], [3], [3]] | |
181 | </pre></div> | |
182 | ||
183 | <P> | |
184 | What has happened is that <code>[[]]</code> is a one-element list containing | |
185 | an empty list, so all three elements of <code>[[]] * 3</code> are (pointers to) | |
186 | this single empty list. Modifying any of the elements of <code>lists</code> | |
187 | modifies this single list. You can create a list of different lists this | |
188 | way: | |
189 | ||
190 | <P> | |
191 | <div class="verbatim"><pre> | |
192 | >>> lists = [[] for i in range(3)] | |
193 | >>> lists[0].append(3) | |
194 | >>> lists[1].append(5) | |
195 | >>> lists[2].append(7) | |
196 | >>> lists | |
197 | [[3], [5], [7]] | |
198 | </pre></div> | |
199 | ||
200 | <P> | |
201 | </DD> | |
202 | <DT><STRONG>(3)</STRONG></DT> | |
203 | <DD>If <var>i</var> or <var>j</var> is negative, the index is relative to | |
204 | the end of the string: <code>len(<var>s</var>) + <var>i</var></code> or | |
205 | <code>len(<var>s</var>) + <var>j</var></code> is substituted. But note that <code>-0</code> is | |
206 | still <code>0</code>. | |
207 | ||
208 | <P> | |
209 | </DD> | |
210 | <DT><STRONG>(4)</STRONG></DT> | |
211 | <DD>The slice of <var>s</var> from <var>i</var> to <var>j</var> is defined as | |
212 | the sequence of items with index <var>k</var> such that <code><var>i</var> <= | |
213 | <var>k</var> < <var>j</var></code>. If <var>i</var> or <var>j</var> is greater than | |
214 | <code>len(<var>s</var>)</code>, use <code>len(<var>s</var>)</code>. If <var>i</var> is omitted, | |
215 | use <code>0</code>. If <var>j</var> is omitted, use <code>len(<var>s</var>)</code>. If | |
216 | <var>i</var> is greater than or equal to <var>j</var>, the slice is empty. | |
217 | ||
218 | <P> | |
219 | </DD> | |
220 | <DT><STRONG>(5)</STRONG></DT> | |
221 | <DD>The slice of <var>s</var> from <var>i</var> to <var>j</var> with step | |
222 | <var>k</var> is defined as the sequence of items with index | |
223 | <code><var>x</var> = <var>i</var> + <var>n</var>*<var>k</var></code> such that | |
224 | <!-- MATH | |
225 | $0 \leq n < \frac{j-i}{k}$ | |
226 | --> | |
227 | <SPAN CLASS="MATH"><IMG | |
228 | WIDTH="88" HEIGHT="37" ALIGN="MIDDLE" BORDER="0" | |
229 | SRC="img1.png" | |
230 | ALT="$0 \leq n < \frac{j-i}{k}$"></SPAN>. In other words, the indices | |
231 | are <code>i</code>, <code>i+k</code>, <code>i+2*k</code>, <code>i+3*k</code> and so on, stopping when | |
232 | <var>j</var> is reached (but never including <var>j</var>). If <var>i</var> or <var>j</var> | |
233 | is greater than <code>len(<var>s</var>)</code>, use <code>len(<var>s</var>)</code>. If | |
234 | <var>i</var> or <var>j</var> are omitted then they become ``end'' values | |
235 | (which end depends on the sign of <var>k</var>). Note, <var>k</var> cannot | |
236 | be zero. | |
237 | ||
238 | <P> | |
239 | </DD> | |
240 | <DT><STRONG>(6)</STRONG></DT> | |
241 | <DD>If <var>s</var> and <var>t</var> are both strings, some Python | |
242 | implementations such as CPython can usually perform an in-place optimization | |
243 | for assignments of the form <code><var>s</var>=<var>s</var>+<var>t</var></code> or | |
244 | <code><var>s</var>+=<var>t</var></code>. When applicable, this optimization makes | |
245 | quadratic run-time much less likely. This optimization is both version | |
246 | and implementation dependent. For performance sensitive code, it is | |
247 | preferable to use the <tt class="method">str.join()</tt> method which assures consistent | |
248 | linear concatenation performance across versions and implementations. | |
249 | ||
250 | <span class="versionnote">Changed in version 2.4: | |
251 | Formerly, string concatenation never occurred in-place.</span> | |
252 | ||
253 | <P> | |
254 | </DD> | |
255 | </DL> | |
256 | ||
257 | <P> | |
258 | <BR><HR><H4>Footnotes</H4> | |
259 | <DL> | |
260 | <DT><A NAME="foot1961">... operations.</A><A | |
261 | href="typesseq.html#tex2html8"><SUP>2.7</SUP></A></DT> | |
262 | <DD>They must | |
263 | have since the parser can't tell the type of the operands. | |
264 | ||
265 | </DD> | |
266 | </DL> | |
267 | <p><br /></p><hr class='online-navigation' /> | |
268 | <div class='online-navigation'> | |
269 | <!--Table of Child-Links--> | |
270 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> | |
271 | ||
272 | <UL CLASS="ChildLinks"> | |
273 | <LI><A href="string-methods.html">2.3.6.1 String Methods</a> | |
274 | <LI><A href="typesseq-strings.html">2.3.6.2 String Formatting Operations</a> | |
275 | <LI><A href="typesseq-xrange.html">2.3.6.3 XRange Type</a> | |
276 | <LI><A href="typesseq-mutable.html">2.3.6.4 Mutable Sequence Types</a> | |
277 | </ul> | |
278 | <!--End of Table of Child-Links--> | |
279 | </div> | |
280 | ||
281 | <DIV CLASS="navigation"> | |
282 | <div class='online-navigation'> | |
283 | <p></p><hr /> | |
284 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
285 | <tr> | |
286 | <td class='online-navigation'><a rel="prev" title="2.3.5 Iterator Types" | |
287 | href="typeiter.html"><img src='../icons/previous.png' | |
288 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
289 | <td class='online-navigation'><a rel="parent" title="2.3 Built-in Types" | |
290 | href="types.html"><img src='../icons/up.png' | |
291 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
292 | <td class='online-navigation'><a rel="next" title="2.3.6.1 String Methods" | |
293 | href="string-methods.html"><img src='../icons/next.png' | |
294 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
295 | <td align="center" width="100%">Python Library Reference</td> | |
296 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
297 | href="contents.html"><img src='../icons/contents.png' | |
298 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
299 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
300 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
301 | <td class='online-navigation'><a rel="index" title="Index" | |
302 | href="genindex.html"><img src='../icons/index.png' | |
303 | border='0' height='32' alt='Index' width='32' /></A></td> | |
304 | </tr></table> | |
305 | <div class='online-navigation'> | |
306 | <b class="navlabel">Previous:</b> | |
307 | <a class="sectref" rel="prev" href="typeiter.html">2.3.5 Iterator Types</A> | |
308 | <b class="navlabel">Up:</b> | |
309 | <a class="sectref" rel="parent" href="types.html">2.3 Built-in Types</A> | |
310 | <b class="navlabel">Next:</b> | |
311 | <a class="sectref" rel="next" href="string-methods.html">2.3.6.1 String Methods</A> | |
312 | </div> | |
313 | </div> | |
314 | <hr /> | |
315 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
316 | </DIV> | |
317 | <!--End of Navigation Panel--> | |
318 | <ADDRESS> | |
319 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
320 | </ADDRESS> | |
321 | </BODY> | |
322 | </HTML> |