A string of Python sequences

Source: Internet
Author: User

I. Definition of sequence 1, data structure: In Python, the most basic data structure is sequence, map 2, index: Each element in the sequence is assigned an ordinal. Note the index defaults from 0   Second, general sequence operations   all sequences can do certain things: Index, shard, add, multiply, check whether an element belongs to a member of a sequence (membership), built-in function (length, minimum, maximum). There is also an important operation here that does not mention the----iteration, which sequentially performs certain operations on each element of the sequence.  2.1 index   All elements in the sequence are numbered----start from 0 and can be accessed by number. Typically, the index value is positive for the week, but Python can also be a plural, representing the counting from right. In the case of a negative number, the first digit at the beginning is 1, not 0, which avoids the coincidence of the first element starting from the left. >>> a = "Hello World" >>> print a[0]h 2.2 slices   shards, the personal understanding is that the variable is obtained by a colon with the corresponding value, and the new value obtained does not change the original value. >>> a = "www.baidu.com" >>> A[0:len (A): 2]    //takes from the first element, takes the last element, and the step is 2. Note that this value can be taken to the last value of the string ' WWBIUCM ' >>> print a    //element value has not been modified www.baidu.com >>> a[-1:0:-2]    //The value, note: This value can only be taken to the first value of the string, but does not include the first value of ' MCUIBW '  2.3 function Method 1. Initial capital letters >>> A.capitalize ()    //capitalize () function to capitalize the first letter ' Www.baidu.com '  >>> data = [' Peter ', ' Paul ', ' MARY ', ' GUIDO ']      >>> [S.capitalize () for s in data]    //The first letter of the list to uppercase [' Peter ', ' Paul ', ' Mary ', ' Guido ']  2. Finding text:In string string, look from the left to the text you want >>> STR4 = "www.baidu.com" >>> str4.find (' Bai ')    //Note: It is necessary to have text in the presence of ' Bai ' keyword is matched and only matches the first keyword 4 >>> str4.find (' Bau ')    //keyword ' bau ' does not match so throws an exception -1 3. CONCATENATE strings: Concatenate the list into strings >>> STR5 = [' www ', '. ', ' Bai ', ' du ', '. ', ' com ']>>> '. Join (STR5)    // The double quotation mark is empty to indicate that a character is not used as the delimiter ' www.baidu.com '   4. Letter conversion commonly used letter conversion str1.lower ()     Convert lowercase letter str1.upper ()     Convert to Capital letter str1.swapcase ()     case swap str1.title ()     Convert to uppercase, other letters lowercase   The corresponding letter judgment method is Str1.islower ()     Judging lowercase str1.isupper ()     Judging uppercase str1.istitle ()     Judging the first letter of the word capitalization  >>> str1 = "AGB" >>> str1.lower ()    //the entire string is converted to lowercase characters ' AGB '  >>> str1 = " ABC ">>> Str1.upper ()    //the entire string is converted to uppercase characters ' ABC '  >>> str1 =" Dadgea ">>> Str1.swapcase ()    //The entire character case is swapped ' dadgea '  >>> str1 = ' alrsof ' >>> str1.title ()    //First LetterThe size of the other characters is changed to lowercase ' alrsof '  5. Find string and replace >>> str1 = "www.qq.com" >>> str1.replace (' QQ ', ' Baidu ', 1)    //1 indicates that only the match is replaced 1 times, if it is not written, it is necessary to replace the ' www.baidu.com '  6 multiple times. Split string to list >>> str1.split ('. ') [' www ', ' qq ', ' com ']    //string "www.qq.com" to "." The number is split to get the relevant value   remarks: This kind of segmentation still feels bad  7. Whitespace handling Str1.strip ([s])     Remove the end and end, you can use the specified parameters to remove the specified parameters Str1.lstrip ([s]     Delete left null, ditto Str1.rstrip ([s])     Delete hostess null, ditto   >>> str1 = "  fdsaf " >& Gt;> Str1.strip ()    //will delete ' Fdsaf '  >>> str1.lstrip ()    //Delete left empty ' fdsaf  '  >>> Str1.rstrip ()    //delete hostess empty '   Fdsaf '

String of Python sequences

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.