Life is short, I learn the magic of Python's Python string

Source: Internet
Author: User
Tags alphabetic character

A ='Wuzemwuzem'#capitalize, the first letter of the string, the rest lowercase. #if the first character of the string is a non-alphabetic character, the original string is returned. The string contains only non-alphabetic word compliance, but returns the original string. such as:v =a.capitalize ()Print(v)#center,20 Indicates the total length,#no blank padding, optionalV1 = A.center (20,'No')Print(v1)#count, statistics, to find the number of ' U ' in the stringV2 = A.count ('u')Print(v2)#EndsWith, string with what end, StartsWith, start with what, return bool valueV3 = A.endswith ('W') V4= A.startswith ('W')Print(V3,V4)#Find , looking back from the beginning, finds the first one, gets its position, cannot find the return-1V5 = A.find ('W')Print(V5)#format, Placeholderv6=A.format ()#Isalnum, judging that only numbers and letters can appear, return boolV7 =A.isalnum () b='alssflk$#%'V8=B.isalnum ()Print(V7)Print(V8)#Isalpha only appear letters, kanji, return boolV11 =A.isalpha ()Print(v11)#expandtabs, the tab symbol in string strings is converted to a space, and the default number of spaces in the tab symbol is 8. SegmentationV9 = A.expandtabs (20) Test='username\temail\tpassword\nw770723324\[email protected]\t12345\nw770723324\[email protected]\t12345\n'V10= Test.expandtabs (20)Print(V10)Print(v9) test='21,342'#Isdecimal,isdecimal to determine if the input is a numberV1 = Test.isdecimal ()#only decimal digits are supportedV2 =test.isdigit ()#contains both decimal decimals and special digital ②V3 = Test.isnumeric ()#Contains both decimal decimals and special numeric ②, including Chinese numeralsPrint(V1,V2,V3)#Swapcase, converting the case of a stringV1 =a.swapcase ()Print(v1)#Isidentifier () determines whether a string is a valid identifier, and the string contains only Chinese text conformance, which in fact determines whether the variable name is valid. Asc ='123ADF'V13=C.isidentifier ()Print(V13) C='_3HF'V13=C.isidentifier ()Print(V13)#str.isprintable (): Determines whether the characters contained in the string are all printable. The string contains a non-printable character, such as an escape character, that returns false. A ='ASDFH\TK'Test=a.isprintable ()Print(test)#isspace judgment is all spacesA ='      'v=a.isspace ()Print(v)#Str.title (): The first letter of each word in the string is capitalized and the remaining lowercase. The first character of a word is a non-alphabetic character and does not affect conversions. The string contains only non-alphabetic word compliance, but returns the original string. such as:A='AB CD'v=A.title ()Print(v)#Istitle to determine if the title format is capitalized in the first letterv=A.istitle ()Print(v)#Str.join (sequence), sequence--sequence of elements to concatenateA ='You're the wind, I'm a fool'b='_'Print(B.join (a)) test='Wuzem'#Ljust, returns an original string left aligned, and uses Fillchar to fill a new string of length width, fillchar default to a space. #Rjust Returns the right alignment of an original string and fills a new string with width of length using Fillchar (default space)V1 = Test.ljust (20,'*') V2= Test.rjust (20,'*')Print(V1,V2)#Islower Determines whether characters are all lowercase#Lower speak string all to lowercasetest='Wuzm'v1=test.islower () v2=Test.lower ()Print(V1,V2)#Isupper Determines whether characters are all uppercase#Upper The string to all uppercasev3=test.isupper () v4=Test.upper ()Print(V3,V4)#rstrip () remove whitespace at the end of a string string to remove the specified stringTest ='Wuzem'v1= Test.lstrip ('W') V2=Test.rstrip () v3=Test.strip ()Print(V1,V2,V3)#Maketrans () creates a conversion table of character mappings, for the simplest invocation of two parameters, the first argument is a string that represents the character that needs to be converted, and the second argument is the target of the string representation of the transformation. #translate () converts string characters according to the table given by STR (256 characters), and the characters to be filtered out into the Deletechars parameterv ='Dhflksha;lsdjfowaa'm= Str.maketrans ('ABCD','1234') New_v=v.translate (m)Print(NEW_V)#Num=string.count (str)) intercepts a string with the Str delimiter, and only the NUM substring if NUM has a specified valueV1=v.split ('s', 2)Print(v1)#returns a tuple of $3, the first is the substring to the left of the delimiter, the second is the delimiter itself, and the third is a substring to the right of the delimiter. V2 = V.partition ('s')Print(v2)#Splitlines ([keepends]) is separated according to the line (' \ r ', ' \ r \ n '),#returns a list containing the rows as elements, if the argument keepends is False, does not contain a newline character, and if true, the newline character is preservedTest ='SDKF\NJHSDF\NHSLSDF'v=test.splitlines (True)Print(v) V2=test.splitlines (False)Print(v2)

Life is short, I learn the magic of Python's Python string

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.