Python string manipulation

Source: Internet
Author: User

Features: cannot be modified

Name.capitalize ()    initial capital Name.casefold ()     uppercase all lowercase name.center ("-")    output   '---------------------alex li----------------------' name.count (' Lex ')   statistics   Lex occurrences Name.encode ()    encodes a string into bytes format name.endswith ("Li")    determines whether the string ends with  li   "Alex\tli ". Expandtabs ()   output ' alex      li ',  convert \ t to how long the space   name.find (' A ')    find A, find returns its index,  not found return -1 format :    >>> msg =   "my name is {}, and age is {}"     >>>  msg.format ("Alex")      ' My name is alex, and age is  22 '     >>> msg =  ' My name is {1}, and  age is {0} "    >>> msg.format (" Alex ")       ' MY NAME IS 22,&NBsp;and age is alex '     >>> msg =  ' my name  is {name}, and age is {age} "    >>>  Msg.format (age=22,name= "ale")      ' My name is ale, and age is  22 ' Format_map    >>> msg.format_map ({' name ': ' Alex ', ' age ': ') '       ' my name is alex, and age is 22 ' Msg.index (' a ')     Returns the index ' 9aA ' of the string where a is located. isalnum ()    true ' 9 '. IsDigit ()   integer name.isnumeric   name.isprintablename.isspacename.istitlename.isupper  "|". Join ([' Alex ', ' Jack ', ' Rain ']) ' Alex|jack|rain ' maketrans    >>> intab =   "Aeiou"    #This  is the string having actual characters.      >>> outtab =  "12345"   #This  is The string having corresponding mapping character    >>>  trantab = str.maketrans (Intab, outtab)     >>>      >>> str =  "This is string example....wow!!!"     >>> str.translate (trantab)      ' th3s 3s  STR3NG 2X1MPL2....W4W!!! '  msg.partition (' is ')     output   (' my name  ',  ' is ',  '  {name},  And age is {age} ')   >>>  "Alex li, chinese name is  lijie ". Replace (" Li "," Li ", 1)       ' Alex li, chinese name is  lijie '  msg.swapcase  case  >>> msg.zfill (+) ' 00000my name is  {name}, and age is {age} ' >>> n4.ljust (+, "-") ' Hello 2orld-----------------------------' >>> n4.rjust (+, "-") '-----------------------------Hello 2orld ' >>>  b= "ddefdsdff_ haha"  >>> b.isidentifier ()   #检测一段字符串可否被当作标志符, that is, whether to conform to the variable naming rules true


This article is from the "Little Rookie" blog, please be sure to keep this source http://baishuchao.blog.51cto.com/12918589/1935005

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.