Python-string-Function

Source: Internet
Author: User

Case-insensitive conversion of characters in a string:

* S. Lower () # lower case

* S. Upper () # uppercase

* S. swapcase () # case-insensitive swap

* S. capitalize () # uppercase letters

* String. capwords (s)

# This is the method in the module. It separates s with the split () function, converts the first letter into upper-case letters with capitalize (), and finally merges them with join ().

* S. Title () # Only the first letter is in upper case, and the other is in lower case. This method is not available in the module.

Alignment of string in output:

* S. Ljust (width, [fillchar])

# The output width is a string of characters, and the S is left aligned. The missing part is filled with fillchar. The default value is space.

* S. Exactly ust (width, [fillchar]) # right alignment

* S. Center (width, [fillchar]) # center alignment

* S. zfill (width) # converts s to width length and alignment on the right. Fill in the missing part with 0.

String search and replacement:

* S. Find (substr, [start, [end])

# Return the number of the first letter of substr in S. If s does not contain substr,-1 is returned. The functions of start and end are equivalent to searching in S [start: end ].

* S. Index (substr, [start, [end])

# It is the same as find (), but a runtime error is returned when S does not contain substr.

* S. rfind (substr, [start, [end])

# Return the number of the first letter of the last occurrence of substr in S. If there is no substr in S,-1 is returned, that is, the first letter of the first occurrence of substr from the right.

* S. rindex (substr, [start, [end])

* S. Count (substr, [start, [end]) # calculates the number of times that substr appears in S.

* S. Replace (oldstr, newstr, [count])

# Replace oldstar with newstr in S, and count with the number of replicas. This is a common form of replacement, and some functions Replace special characters.

* S. Strip ([chars])

# Remove all characters in Chars before and after S, which can be understood as replacing chars before and after s with none

* S. lstrip ([chars])

* S. rstrip ([chars])

* S. expandtabs ([tabsize])

# Replace the Tab character in S with no space. Each tab is replaced with tabsize with eight spaces by default.

String segmentation and combination:

* S. Split ([SEP, [maxsplit])

# Use Sep as the separator to divide s into a list. Maxsplit indicates the number of splits. The default Delimiter is a blank character.

* S. rsplit ([SEP, [maxsplit])

* S. splitlines ([keepends])

# Divides s into a list according to the row delimiter, and keepends is a bool value. If it is true, the row delimiter is retained.

* S. Join (SEQ) # Concatenates the sequence represented by seq, namely the string sequence, with S

String mapping. This function contains two functions:

* String. maketrans (from,)

# Returns a 256-character translation table, where the characters in from are converted to, so the from and to must be of the same length.

* S. Translate (Table [, deletechars])


# Use the post-production translation table of the above function to translate S and delete some characters in deletechars. Note that if S is a unicode string, It is not supported
The deletechars parameter can be used to translate a character into none to achieve the same function. In addition, you can use the functions of the codecs module to create more powerful translations.
Table.

The string also has a pair of encoding and decoding functions:

* S. encode ([encoding, [errors])

# Encoding can have multiple values, such as gb2312 GBK gb18030 bz2 zlib big5
Bzse64 and so on are supported. The default errors value is "strict", which means unicodeerror. Possible values include 'ignore ',
'Replace ', 'xmlcharrefresh', 'backslashreplace'
And all values registered through codecs. register_error. This part of content involves the codecs module, not the plain white

* S. Decode ([encoding, [errors])

String test functions, which are not in the string module. These functions return bool values:

* S. startwith (prefix [, start [, end])

# Whether to start with prefix

* S. endwith (suffix [, start [, end])

# End with suffix

* S. isalnum ()

# Whether it is all letters and numbers with at least one character

* S. isalpha () # whether it is all letters with at least one character

* S. isdigit () # whether it is all numbers with at least one character

* S. isspace () # whether it is all blank characters with at least one character

* S. islower () # whether all letters in s are in lower case

* S. isupper () # Whether the letters in S are uppercase letters

* S. istitle () # whether s is capitalized

String type conversion functions. These functions are only available in the string module:

* String. atoi (s [, base])

# Base is 10 by default. If it is 0, s can be a string in the format of 012 or 0x23. If it is 16, s can only be a string in the format of 0x23 or 0x12.

* String. atol (s [, base]) # convert it to long

* String. atof (s [, base]) # converts it to float

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.