Python string processing

Source: Internet
Author: User

String processing is definitely the focus of any language.

str. partition (Sep)

Split the string at the first occurrence of Sep, and return a 3-tuple containing the part before the separator, T He separator itself, and the part after the separator. If The separator is not found, return a 3-tuple containing the string itself, followed by both empty strings.

 mystr =  " hello,!!! World   " print  (Mystr.partition ( Span style= "COLOR: #800000" > " ,  "  Span style= "color: #000000"))  #   output  ( " hello   ' , < span>,  '  !!! World   ") 
str. split (sep=none, maxsplit=-1)

Return A list of the words in the string, using Sep as the delimiter string. If Maxsplit is given, at most maxsplit splits be done (thus, the list would have at most maxsplit+1 elements ). If maxsplit is isn't specified or -1 , then there was no limit on the number of splits (all possible splits are Made).

str. join (iterable)

Return a string which is the concatenation of the strings in iterable. A'll be TypeError raised if there is any non-string values in iterable, including bytes objects. The separator between elements is the string providing this method.

 mylist = [ " first  " ,  " second  " ,  " third   " ]mystr  = "  #   " .join (mylist)  print   (MYSTR)  #   Output:first#second#third  
str. startswith (prefix, start, end)

Return True If string starts with the prefix, otherwise return False . prefix can also is a tuple of prefixes to look for. With optional start, the test string beginning at that position. With optional end, stop comparing string at that position.

" This is a string EXAMPLE....WOW!!! " Print (Mystr.startswith ('this')) Print (Mystr.startswith ('is', 2, 4)) Print (Mystr.startswith ('this', 2, 4))

The output is as follows:

True
True
False

Python string processing

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.