"Python" String functions

Source: Internet
Author: User

1, str. Replace(word0,word1) # #用word1替换str中所有的word0

>>> ' Tea for too '. Replace (' too ', ' both ') output: ' Tea for both '

2, str. Lower() # #字符串str中的字母all converted to lowercase

STR. Upper() # #字符串str中的字母convert all to uppercase

Str. Capitalize() # #字符串str中的first letter converted to uppercase

>>> ' df Dfs ds '. Upper () ' DF Dfs ds ' >>> ' sdf SF omp dfs '. Lower () ' SDF SF omp dfs '
>>> ' df Dfs ds '. Capitalize ()
' Df DFS ds '

3. Str. Split(Sep) # # to divide str by Sep by default when Sep is sliced according to Whilespace (Space, tab, newline, return, FormFeed)

>>> ' df Dfs ds '. Split () [' DF ', ' Dfs ', ' ds ']>>> ' Df,dfs,ds '. Split (', ') [' DF ', ' Dfs ', ' DS ']

----< continued >---------

Using Regular expression to pass the distribution processing

>>> Import re
Re.findall (R ' \bf[a-z]* ', ' which foot or hand fell fastest ')
Output: [' foot ', ' fell ', ' fastest ']
re.sub (R ' (\b[a-z]+) \1 ', R ' \1 ', ' Cat in the The Hat ')
Output: ' Cat in the Hat '



"Python" String functions

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.