Operations related to python strings and python strings

Source: Internet
Author: User

Operations related to python strings and python strings

 

Python string-related operations

 

1. string SEARCH
Name = "today is a \ t beautiful day"
Print (name. capitalize () # uppercase letters
Print (name. center (40, '-') # center display, fill in '-' when there is not enough '-'
Print (name. count ('A', 4) # count the number of times a character appears
Print (name. endswith ('A') # judge whether it ends with a given character. True is returned; otherwise, False is returned.
Print (name. startswith ('T') # returns True if it starts with a given character. Otherwise, False is returned.
Print (name. expandtabs (30) # Set the tab Length
Print (name. find ('is') # search for the index of the first occurrence of a string
Print (name. rfind ('A') # index of the last occurrence of a given character
Print (name. index ('A') # index of the first occurrence of a given character
Print (name. rindex ('A') # index of the last occurrence of a given character
Print (name. swapcase () # small to large case conversion, large to small
Print (name. replace ('s ','s') # string replacement
Print (name. title () # uppercase letters
Print (name. lower () # All lowercase letters
Print (name. upper () # all uppercase letters


2. String judgment
Name3 = '123afdasd'
Print (name3.isdigit () # determine if it is an integer
Print (name3.isalnum () # whether it is a letter or number, Arabic
Print (name3.isalpha () # Is it a letter?
Print (name3.isdecimal () # Is it a decimal number?
Print (name3.isidentifier () # identifies a valid identifier.
Print (name3.isnumeric () # Is it a number?
Print (name3.islower () # whether it is all lowercase
Print (name3.isupper () # whether it is fully capitalized
Print (name3.istitle () # whether the first letter is capitalized
Print (name3.isprintable () # whether it can print characters
Print (name3.isspace () # whether it is a space

3. String Filling
Print (name. ljust (30, '-') # Fill in '-' on the left to reach the specified number of characters
Print (name. Fill ust (30, '-') # Fill '-' to the left of the string to the specified number of characters.
Print (name. zfill (30) # Fill '0' on the right side of the string to reach the specified number of characters.

4. String space Processing
# Remove the given characters (beginning or end). The default values are \ t, \ r, \ n, and ''.
Name = 'asdffgas'
Print (name. lstrip ('as') # Remove the left space
Print (name. rstrip ('as') # Remove the right space
Print (name. strip ('as') # Remove spaces on both sides

5. string segmentation

Print (name. partition ('D') # split the string into three parts after the separator.
Print (name. rpartition ('D') # split the string into three parts after the separator.

Print (name. rsplit ('D') # split the string into the first and last two parts based on the delimiter, excluding the delimiter
Print (name. split ('D') # split the string into the first and last two parts based on the delimiter, excluding the delimiter

 

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.