Python string manipulation

Source: Internet
Author: User

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 #Auther:summer4 5 #string Manipulation6Name ="Summer Du"7 Print(Name.capitalize ())#capitalize the initial letter8 Print(Name.casefold ())#uppercase letters All lowercase9 Print(Name.center (50,"="))#Output ====================summer du=====================Ten Print(Name.count ("m"))#Count M occurrences One Print(Name.encode ())#encode a string into bytes format A Print(Name.endswith ("m"))#determines whether a string ends in M - Print(Name.find ("D"))#Find D, find returns its index value, cannot find return-1, return it to find the first index value -  theName ="Sum\tmer Du" - Print(Name.expandtabs (10))#Output sum mer Du -  - #format +msg ="My name is {},and-is {}" - Print(Msg.format ("Summer"," -")) +msg ="My name is {1},and-is {0}" A Print(Msg.format ("Summer"," A")) atmsg ="My name is {name},and ' {age}" - Print(Msg.format (name ="Summer", age =" A")) - Print(Msg.format (age =" A", name ="Summer")) -  - #Format_map - Print(Msg.format_map ({' Age': 22,'name':"Summer"})) inmsg ="My name is {name},and ' {age}" - Print(Msg.index ("m")) to  + Print("9Aa". Isalnum ()) - Print("9". IsDigit ())#whether integer the Print(Name.isnumeric ()) * Print(Name.isprintable ()) $ Print(Name.isspace ())Panax Notoginseng Print(Name.istitle ()) - Print(Name.isupper ()) theName ="Summer Du" + Print(Name.join ("Lee")) A Print("|". Join (["Lee","Summer"]))#put it in the list. the  + #the Maketrans Python Maketrans () method is used to create a conversion table for character mappings. - #for the simplest invocation method that accepts two parameters, the first argument is a string that represents the character that needs to be converted, $ #The second parameter is also the target of the string representation transformation.  $Intab ="Aeiou" -Outtab ="12345" -Trantab =Str.maketrans (Intab,outtab) the Print(Trantab) - Wuyi #Partition the Print(Msg.partition (" is")) -  WuR ="My name is Summer,and" - Print(R.replace ("m","N", 2)) About  $ Print(Name.swapcase ())#uppercase and lowercase swaps -  - Print(Msg.zfill (40)) - Print(Msg.ljust (40,"-")) A Print(Msg.rjust (50,"=")) + Print(Msg.center (50,"=")) the  -b ="Ddefs;ajfdal;_haho (∩_∩) o haha ~" $c ="dd_ haha" the Print(B.isidentifier ())##检测一段字符串可否被当作标志符, that is, whether to conform to variable naming rules the Print(C.isidentifier ())##检测一段字符串可否被当作标志符, that is, whether to conform to variable naming rules

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.