07-python-string manipulation

Source: Internet
Author: User

1. Common string manipulation

1Name ='My name is Druid'2 3 Print(Name.capitalize ())#Capitalize first letter4 Print(Name.count ('D'))5 Print(Name.center (50,'-'))#A total of 50 characters are printed, the string is placed, and the bits are not enough-filled6 Print(Name.ljust (50,'*'))#print a total of 50 characters, string left, not enough bit with * fill7 Print(Name.rjust (50,'~'))#print a total of 50 characters, string to right, not enough bits to fill with ~8 Print(Name.endswith ('D'))#whether to end with the string ' d '9 Print(Name.expandtabs (tabsize=30))#Tab key is 30 spacesTen Print(Name.find ('D'))#returns the first index of a character, not found returns 1 One Print(Name[name.find ('D'):])#slices A Print('Druid'. RFind ('D'))#returns the index of the last character - Print(Name.index ('D'))#returns the index, no error is found - Print(Name[name.index ('D'):])#slices the  -s ='a123a' - Print(S.isalnum ())#whether it is alphanumeric - Print(S.isalpha ())#Whether it is a plain English character + Print(S.isdecimal ())#whether it is a decimal - Print(S.isdigit ())#whether it is an integer + Print(S.isidentifier ())#whether it is a valid identifier (variable name) A Print(S.islower ())#whether it is lowercase at Print(S.isnumeric ())#whether there are only numbers - Print(S.istitle ())#whether the beginning of each word is uppercase - Print(S.isupper ())#is uppercase - Print("'. Join ('druid! ')) )#connect an iterative object with a string in ' ' - Print('+'. Join (['a','b','C'])) - Print('DruiD'. Lower ())#lowercase in Print('Druid'. Upper ())#Uppercase -  to Print('\ Druid'. Lstrip ())#Remove left space and line break + Print('druid \ n'. Rstrip ())#Remove right space and line break - Print('\ Druid \ n'. Strip ())#remove spaces and line breaks on both sides the  *p = Str.maketrans ('ABCDE','12345')#the preceding string is replaced with the following string $ Print('ABCDE'. Translate (p))#if the preceding string and the string in P have the same character, replace thePanax Notoginseng Print('Druid ABCDE'. Translate (p))#The characters in the preceding string and the string in P are not replaced -  the Print('Druid'. replace ('D','D', 1))#Replace the first character, and if you do not add 1, replace all +  A Print('Druid Chenc'. Split ())#by default, the characters are separated by a space, resulting in a list the Print('Druid Chenc'. Split ('D'))#use D as delimiter + Print('Druid Chenc'. Split ('C'))#with C as the delimiter - Print('1+2+3'. Split ('+'))#with + as delimiter $ Print('1+2\n+3+4'. Splitlines ())#delimited by line break $  - Print('Druid Chen'. Zfill (50))#left with 0 padding -  theName1 ='My name is {name},age {age}' - Print(Name1.format (name ='Druid', age = 18))Wuyi Print(Name1.format_map ({'name':'Alex',' Age': 33}))

07-python-string manipulation

Related Article

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.