Python3 String Operations Summary

Source: Internet
Author: User

    • string Interception  
' Hello '>>>s[0:3' 'he'# intercepts all characters '  Hello'
    • eliminate spaces and special symbols          
S.strip ()#eliminate whitespace characters on both sides of the string s (including ' \ t ', ' \ n ', ' \ R ', ')S.strip ('0')#eliminate special characters (such as ' 0 ') in the left and right sides of the string, and ' 0 ' in the middle of the string will not be deletedFor example:>>>s ='000hello00world000'>>>s.strip ('0')'Hello00world'S.strip (' A') is equivalent to S.strip (' +') For example:>>>s ='12hello21'>>>s.strip (' A')'Hello'Lstrip,rstrip usage is similar to strip, which is used to eliminate left and right characters, respectively.
    • String copy
' Hello '  #  s2 = ' Hello' 'hello'#  S2 = ' he '
    • String Connection  
' Hello '  'World'= s1 + s2  #s3 = ' HelloWorld '  or  Import#concat is a string concatenation function
    • string comparison
(1) using the Operator module method comparison (Python3. X cancels the CMD function) contains the method: Lt (A, B) ———— less than LE (A and B) ———— less than or equal to EQ (A, b) ———— equals NE (A, B) ———— not equal to GE (A, b) ———— greater than or equal to GT (A, B) ———— greater than the example: >>>Importoperator>>>operator.eq ('ABC','EDF')#Comparison by ASCII codeflase&GT;&GT;&GT;OPERATOR.GT ('ABC','AB') True (2) relational operator comparison (>,<,>=,<=,==,!=)&GT;&GT;&GT;S1 ='ABC'&GT;&GT;&GT;S2 ='AB'>>>s1 >s2true&GT;&GT;&GT;S1 = =S2false
    • To find the string length
' Hello '>>>len (S1)5
    • Maximum character, minimum character in string
' Hello ' # find the maximum character in a string S1 ' o ' # To find the minimum character in a string S2 ' e '
    • String Case Conversion  
The main methods are as follows: Upper ———— converted to uppercase lower ———— converted to lowercase title ———— converted to a title (capitalized per word) Capitalize ———— first letter capital Swapcase ———— uppercase to lowercase, lowercase to uppercase example :&GT;&GT;&GT;S1 ='Hello'&GT;&GT;&GT;S2 =' World'&GT;&GT;&GT;S3 ='Hello World'>>>S1.upper ()'HELLO'>>>S2.lower ()' World'>>>S3.title ()'Hello World'>>>s3.capitalize ()'Hello World'>>>s3.title (). Swapcase ()'HELLO World'
    • String flipping
' Hello ' >>>s1[::-1]'olleh'
    • String segmentation
Split method, split according to the parameters, returns a list

Example:
' Hello,world '>>>s1.split (',') ['hello', ' World ']
    • String Sequence Connection  
Join method:     # seq is a sequence of elements  >>>l = ['hello','World'] ' - '>>>str.join (l)'hello-world'
    • Find inside strings
 find method: Detects if the string contains substrings within the STR syntax: Str.find (Str[,start,end])  #  str is the string to look for, Strat to find the starting position, the default is 0;end to find the terminating position, and the default is the string length. Returns 1   example:  >>>s1 =  If it is found to return the starting position index  today is a fine day   " >>> S1.find ( '  is   " )  6>>>s1.find ( " is  " , 3)  6>> >s1.find ( '  is   ", 7,10) -1 
    • Intra-string substitution
Replace method: Replace the old strings in the string with the new string syntax:     # old, new string, Max Optional, number of replacements Example: ' today is a Find day '>>>s1.replace ' ('find','rainy'  )'today is a rainyday'
    • Judging string composition
the main methods are as follows: IsDigit ———— The detection of strings is only composed of numbers Isalnum ———— detect whether the string is only composed of numbers and letters Isalpha ———— the detection string is only composed of letters Islower ———— Detects if the string contains only lowercase letters isupper ———— detect if the string contains only uppercase letters ISSPACE ———— detect if the string contains only spaces Istitle ———— detect if the string is a title (capitalize each word) Example:>> >s1  'hello'>>>s1.islower () True>>>s1.isdigit ( ) False

Python3 String Operations Summary

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.