Python practice String (i): Connect, cut, case, compose

Source: Internet
Author: User

String:
1. is a sequence of characters consisting of a single character, which is a collection of characters
2. String is an immutable object
3. Sequence of characters with single quotes, double quotes, and triple quotes
In 4.python3, strings are Unicode types, There are two kinds of Unicode in 2
A non-Unicode
string element Access--Subscript:
1. String support using indexed access
2. Ordered character set, character sequence
3. is an iterative
String Join connection:
1. Strings can be stitched together, such as
(Repeat stitching), + (two stitching into a new one)
2. Concatenate an iterative object, string as a delimiter
3. The object itself can be iterated as the element should be a string
4. Return a new string
5.string.join (iterable), str
Example:. A = List (range (5))
', '. Join (a) #将list: A with a comma as a delimiter and join it together
Because a is an int non-STR will report TypeError

Print (', '. Join (Map (Str,range (5))) #所以, you should convert the Iterator object range (5) to Str when joining with join
String segmentation:
Split
1. From left to right
2.SEP specifies a split string, by default a blank string as a delimiter
3.MAXSPLIT Specifies the number of splits, 1 means traversing the entire string
4. Return to List
5.split (Sep = none,maxsplit=-1)->list of strings
Rsplit:
1. From left to right
2.sep, Maxsplit and split define the same
3.rsplit (sep=none,maxsplit=-1)-> List of strings
Splitlines:
1. From left to right
2.keepends, specifies whether the row delimiter is preserved
3. Line delimiter includes \ n, \ r \ r, etc.
4.splitlines ([keepends])->list of string
Partition
1. From left to right, encounter delimiter to divide the string into two parts, return header, delimiter, tail three parts of the ternary group; If no delimiter is found, it returns the triples of the header, 2 empty elements
2.sep split string, you must specify
3.partition (Sep)-> (Head.sep.tail)
Rpartition:
1. From right to left, encounter the delimiter to divide the string into two parts, return the head, delimiter, tail three parts of the ternary group; If a delimiter is not found, it returns 2 empty elements and a trailing ternary group
String case:
Upper ()
All caps
Lower ()
All lowercase
Swapcase
case, use when making judgments
Interactive case
String composition:
1.title ()->str
Each word of the title is case-sensitive
2.capitalize ()-> str
First word capitalization
3.center (Width[,fillchar])->str
Width print widths
Fillchar-filled characters
4.zfill (width)->str
Width print, center, left with 0 padding
5.ljust (Width[,fillchar])->str left-justified
6.rjust (Width[,fillchar])->str right align

Python practice String (i): Connect, cut, case, compose

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.