Python_ string manipulation

Source: Internet
Author: User

1 name='hu\tqihang' #\ t is the TAB key

1. Printing related

1 Print(Name.center (50,'-'))#print a total of 50 characters, not enough to fill, name in the middle2 Print(Name.ljust (50,'-'))#print a total of 50 characters, not enough for-fill, name on the left3 Print(Name.rjust (50,'-'))#print a total of 50 characters, not enough for-fill, name on the right4 Print(Name.zfill (50))#print a total of 50 characters, not enough with 0 to fill in front

2. Find the relevant

1 Print(Name.count ('a'))#数A number of occurrences2 Print(Name.find ('Qi'))#find the leftmost qi start subscript3 Print(Name.rfind ('h'))#Find the subscript of the rightmost H4 Print(Name[name.find ('Qi'):])#find handy string slices5 Print(Name.index ('Qi'))#same as find, except if STR does not report an exception in string

3. Determine the relevant

1 Print(Name.startswith ('Hu'))#determines whether a string starts with Hu2 Print(Name.endswith ('ng'))#determines whether a string ends with Ng3 Print('abcABC123'. Isalnum ())#determine if it is an English character or number4 Print('ABCABC'. Isalpha ())#determine if it is a plain English character5 Print('1234'. IsDigit ())#determines whether a string is made up of numbers only6 Print('ABC'. Islower ())#Judging if it's all lowercase7 Print('ABC'. Isupper ())#Judging whether it's all uppercase8 Print('is Title'. Istitle ())#determine if each initial letter is capitalized9 Print(' '. Isspace ())#determine if it is a spaceTen Print(' +'. Isdecimal ())#determines whether a string contains only decimal characters One Print('name'. Isidentifier ())#determine if the variable name is valid

4. Change-related

1 Print(Name.capitalize ())#capitalize the first letter2 Print('is title'. Title ())#capitalize it every first letter3 Print('Huqihang'. Lower ())#Turn uppercase into lowercase4 Print('Huqihang'. Upper ())#Turn lowercase into uppercase5 Print('Hu Qihang'. Swapcase ())#uppercase to lowercase, lowercase to uppercase6 Print(Name.expandtabs (tabsize=30))#Turn the TAB key into 30 spaces7 Print('\nhuqihang\n'. Strip ())#Remove all carriage returns.8 Print('\nhuqihang'. Lstrip ())#Remove the carriage return to the left9 Print('huqihang\n'. Rstrip ())#Remove the right carriage returnTen Print('+'. Join (['1','2','3']))#generates a new string from the elements in the sequence with the specified character connection One Print('1+2+3+4+5+6'. Split ('+'))#divide the string into a list, + as a delimiter, not in the list, Name.split () By default is a space A Print('1+2+3\n+4+5+6'. Splitlines ())#Break into lists by line breaks

5. Replace the relevant

1 P=str.maketrans ('abc','123')2  Print(Name.translate (p))                # replaces ABC in name with 123

1 print(name.replace ('h','h', 1)) # replaces the h in name with H, replacing only one

Python_ string manipulation

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.