Python core Programming (2)-Sequence: strings, lists, and tuples

Source: Internet
Author: User
Tags character classes chr printable characters

1. Sequence

The sequence type has the same access pattern: each of its elements can be obtained by specifying an offset. Multiple elements can be obtained one at a time by slicing operations. The subscript offset is from 0 to the total number of elements-1 end-the reason is to subtract one because we are counting from 0.

Sequence type operator
Sequence operator Role
Seq[ind] Get the element labeled IND
SEQ[IND1:IND2] Gets the set of elements from Ind1 to Ind2
SEQ * Expr Sequence repeats expr times
SEQ1 + SEQ2 Connecting sequences seq1 and SEQ2
obj in seq Determine if the obj element is included in the SEQ
Obj not in seq Determine if the obj element is not included in the SEQ

The member relationship operator (in, not): the member relation operator uses to determine whether an element belongs to a sequence. The return value of the In/not in operator is generally true/false, which returns true if the member relationship is satisfied, otherwise False is returned. The syntax for this operator is as follows:obj [not] in sequence

Join operator (+): This operator allows us to concatenate a sequence with another sequence of the same type. The syntax is as follows:Sequence1 + sequence2. The result of the expression is a new sequence containing the contents of Sequence1 and Sequence2.

repeat operator (*): The repetition operator is useful when you need multiple copies of a sequence, and its syntax is as follows:sequence * copies_int. the operator returns a new object with multiple copies of the original object.

slice operator ([],[:],[::]): The so-called sequence type is a structure that contains some ordered objects. You can simply access each of its elements in square brackets with a subscript, or access a contiguous set of elements by separating the starting subscript and ending subscript in square brackets.

A sequence type is a type of data structure whose elements are placed sequentially, which allows a data element to be obtained by specifying a subscript, or to obtain an element of a set of sequences by specifying a subscript range. This way of accessing the sequence is called slicing, and we can do what we said by slicing the operator. The syntax for accessing a data element is as follows:Sequence[index]. Sequence is the name of the sequence, and index is the offset of the element you want to access. The offset can be positive, ranging from 0 to the maximum offset (one less than the length of the sequence), with the Len () function, the sequence length can be obtained, the actual range is 0 <= Inde <= len (sequece)-1. Alternatively, a negative index can be used, ranging from 1 to the negative length of the sequence,-len (sequence),-len (sequence) <= index <=-1. The difference between positive and negative indexes is that the positive index starts with the beginning of the sequence and the negative index begins with the end of the sequence.

Because Python is object-oriented, you can directly access a sequence of elements as follows (without first assigning it to a variable):

>>> print (' Faye ', ' Leanna ', ' Daylen ') [1]
Leanna

What if you want to get more than one element at a time? Simply give the start and end index values, separated by a colon, with the following syntax:Sequence[starting_index:ending_index]. in this way we can get a "slice" element between the starting index and the end index (excluding the element corresponding to the end index). Both the start and end indexes are optional, and if none is provided or the index value is not used, the slice operation starts at the beginning of the sequence. or until the end of the end of the sequence. The last slice of the sequence is an extended slice operation, and its third index value is used as a step parameter.

The built-in function list (), str () and tuple () are used to convert between various sequence types.

Sequence Type conversion factory function
Function Meaning
List (ITER) Convert an iterative object to a list
STR (obj) Converts the Obj object to a string (the string representation of the object)
Unicode (obj) Converts an object to a Unicode string (using the default encoding)
Basestring ()

Abstract factory functions, which function only to provide a parent class for STR and Unicode functions, cannot be
instantiated, nor can it be called (

Tuple (ITER) Convert an iterative object to a tuple object

Built-in functions available for sequence types
Name of function Function
Enumerate (ITER)

Takes an iterative object as a parameter, returning a enumerate object (also an iterator) that generates a tuple of index and item values for each element of ITER (PEP 279)

Len (seq) Returns the length of the SEQ

Max (Iter,key=none)

0r Max (Arg0,arg1......,key=none)

Return to ITER or (Arg0,arg1,...) If a key is specified, the key must be a value that can be passed to the sort () method for comparison
Tuning function.

Min (Iter,key=none)

or min (arg0,arg1......,key=none)

Return the minimum value inside ITER; or return (Arg0,arg2,...) Inside

If a key is specified, the key must be a value that can be passed to the
The sort () method that is used to compare the callback function.

Reversed (SEQ) Takes a sequence as a parameter and returns an iterator that is accessed in reverse order (PEP 322)
Sorted (Iter,func=none,key=none,reverse=false)

Takes an iterative object as a parameter, returns an ordered list, the meaning of the optional parameters Func,key and reverse, and the parameter meaning of the List.sort () built-in function.
Sample.

SUM (seq,init=0)

Returns the sum of the SEQ and optional parameter init, which has the same effect as reduce (operator.add,seq,init)

Zip ([it0,it1,...... ItN])

Returns a list whose first element is It0,it1,... The first element of these elements consists of a tuple, a second ..., and so on.

2. String

The string type is the most common type in Python. We can simply create it by including the characters in the quotation marks. In Python, single and double quotes work the same. A string is an immutable type, meaning that an element that changes a string needs to create a new string. The strings are composed of separate characters, and these characters can be accessed sequentially through the slice operation.

Creating a string is as simple as using a scalar, and the string is followed by a square bracket with one or more indexes to get the substring. "Update" an existing string by assigning (or re-assigning) a variable. The new value may be similar to the original value, or it may be completely different from the original string. As with the numeric type, the string type is immutable, so you have to change a string by creating a new one. That means you can't just change a character or a substring of a string, but it's permissible to get a new string by piecing together the pieces of an old string.

The string is immutable, so you can't just delete a character in a string, you can either empty or delete a string by assigning an empty string or using the DEL statement.

3. Strings and operators

When you forward an index , the index value starts at 0 and ends at the total length minus 1 (because we are indexed from 0). Within this range, we can access any substring. Calling the slice operator with one parameter results in a single character, while a parameter that uses a range of values (': ') to invoke a slice operation as a parameter returns a string of contiguous characters. Again, for any range [Start:end], we can access all the characters that include start inside to end (not including end), in other words, assuming X is an index value in [Start:end], then there are: Start<= x < end.

In the case of a reverse index operation, starting at 1, counting the starting direction of the string, the negative number of the string length is the end of the index (-len (str)). If the start index or the end index is not specified, the first and last index values of the string are the default values, respectively.

Note : If neither the start/end index is specified, the entire string is returned.

The member operator (in, not): the member operator is used to determine whether a character or a substring (the characters in) appears in another string. Returns True if it appears, otherwise false.

Note: The member operator is not used to determine whether a string contains another string, which is done by the find () or index () (and their brother: RFind () and Rindex ()) functions.

connector (+): run-time string connection. Gets a new string from the original string by using the join operator.

Compile-time string connections, the Python syntax allows you to concatenate several strings together in the source code to construct a new string. There are two types of quotes that can be mixed in one line, and the advantage is that you can add the annotations as well.

If you concatenate a normal string with a Unicode string, Python converts the normal string to a Unicode string before the connection operation.

The repeating operator creates a new string containing multiple copies of the original string.

4. Operators that apply only to strings

format operator (%): applies only to string types. Ex:print ("str%s%d"% (name,20)) = "str name 20

string formatting symbols
formatting characters How to convert
% c converted to characters (ASCII value, or string of one length)
%r takes precedence over string conversions with the repr () function
%s takes precedence over string conversions with the str () function
%d  %i to a signed decimal number
%u turns unsigned decimal number
%o turns unsigned octal number
%x  %x

(Unsigned) to unsigned hexadecimal number (x/x for the case of the converted hexadecimal character)

%e  %e turn scientific notation (e/e control output e/e)
%f    %f to floating-point numbers (natural truncation of fractional parts)
%g  %g %e and%f/%e and%f abbreviations
% output%

The format string can be used with the print statement to output data to end users, and can be used to combine strings to form new strings, and can also be displayed directly to the GUI (graphical user Interface) interface.

Formatting operator Auxiliary directives
Symbol Role
* Define width or decimal precision
- Used for left justification
+ Show plus sign (+) in front of positive number
<sp> Show spaces in front of positive numbers
#

Displays 0 (' 0 ') before the octal number, preceded by ' 0x ' or ' 0X ' in hexadecimal (depending on ' x ' or ' x ')

0 The displayed number is preceded by ' 0 ' instead of the default space
% ' percent ' output a single '% '
(VAR) mapping variables (dictionary parameters)
M.n M is the minimum total width displayed, and n is the number of digits after the decimal point (if available)

The Template object has two methods, substitute () and Safe_substitute (). The former is more rigorous, in the case of key missing it will report a
A Keyerror exception, while the latter, in the absence of a key, is directly intact to display the string.

Primitive string operator (R/R): in the original string, all characters are used directly in the literal sense, and no special or non-printable characters are escaped. Regular expressions are strings that define advanced search matches, usually consisting of special symbols representing characters, groupings, matching information, variable names, and character classes. The ' r ' can be lowercase or uppercase, and the only requirement is that it must be immediately before the first quotation mark.

Unicode string operator (u/u): converts a standard string or a string containing Unicode characters to a fully Unicode string object. The Unicode operator must appear before the original string operator

5. Built-in functions

CMP (): As with the comparison operator, the built-in CMP () function is also compared against the ASCII value of the string.

Len (): The built-in function Len () returns the number of characters in a string.

max () and Min (): for string types they work well and return the maximum or minimum characters, arranged in ASCII values.

Enumerate ():>>> s = ' foobar '

>>> for I, T in enumerate (s):
.. print I, t
...
0 F
1 o
2 O
3 b
4 A
3 4

zip ():>>> s, t = ' Foa ', ' OBR '

>>> zip (S, t)
[(' F ', ' O '), (' O ', ' B '), (' A ', ' R ')]

raw_input (): the Built-in raw_input () function prompts the user for input with the given string and returns the input.

STR () and Unicode ():the str () and Unicode () functions are factory functions that produce objects of the corresponding type. They accept an arbitrary type of object and then create a printable or Unicode string representation of the object. They and basestring can be passed as arguments to the isinstance () function to determine the type of an object.

chr (), UNICHR (), and Ord(): the Chr () function uses a range (0 to 255) integer parameter to return a corresponding character. UNICHR () is just like it, except that it returns a Unicode character,

Python core Programming (2)-Sequence: strings, lists, and tuples

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.