Records for Python2.7 processing string methods

Source: Internet
Author: User

For the recent learning of Python string and array operation of the preliminary knowledge, the following summary:

First, it needs to be clear: the string is immutable and the array can change arbitrarily.

defines a string : a= "index" or a= ' index '; double and single quotes can be nested with each other, and do not need to be escaped

Connect two strings:a+b or A + "This is a test"; note the type of element being added, need to transform the need to add str () function transformation

extended output string:a*3; the output is 3 identical strings to the end of each other

nested variable:"This is a test%s"%d; the value of the variable d is nested at the string%s location

gets the string inner element : A[i],i represents the string subscript, starting with 0, the maximum is Len (a)-1, and the value of I can be negative, the corresponding value is the inverse of the I element position; Therefore, the subscript value range is-len (a) to Len (a)-1

Intercept string Content : A[m:n],m is the starting subscript, empty defaults to 0;n to end subscript, and null defaults to the maximum value. pay attention to the principle of taking the number: Take the left not to the right; Similarly, the subscript values for M and n can be negative, and the order is still reversed

Character-Case Conversions:

Upper () converts the string to uppercase;

Lower () converts the string to lowercase;

Capitalize () capitalize the first letter

Note: These actions do not change the original string

String judgment:

Isupper () Determines whether the string is all uppercase, otherwise returns false;

Islower () Determines whether the string is all lowercase, otherwise returns false;

Istitle () determines whether the first letter of the string is uppercase, otherwise returns false. What you need to explain here is that if there are different separators in the string (. "", etc.), the subsequent initials must also be capitalized, as

b="Test Window"#FalseC="test. Window"#FalseD="Test.window"#FalseE="Test.window"#True    PrintB.istitle ()PrintC.istitle ()PrintD.istitle ()PrintE.istitle ()

If B.capitalize (). Istitle (), the return value is still false

To remove whitespace before and after a string:

Strip (), this method can also remove the tab stops before and after the string, line breaks and other whitespace characters

Lstrip () to remove whitespace to the left of the string

Rstrip () to remove whitespace to the right of the string

Records for Python2.7 processing string methods

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.