Python character operation functions

Source: Internet
Author: User
Tags uppercase letter

There are various string operation functions in Python. In history, the string class experienced a cycle in Python. At the very beginning, Python had a special string module. To use the string method, we had to import it first. However, due to the suggestions of many Python users, we started from python2.0, the string method is changed to S. method (), as long as S is a string object, it can be used in this way, without the need for import. At the same time, to maintain backward compatibility, the current Python still retains a string module, and the defined method and S. method () is the same, and all these methods finally point to S. method. Note that S. Method () can call more methods than the string module. For example, isdigit () and istitle () can only be called in the S. Method () method.

For a String object, the first thought of operation may be to calculate the number of characters it contains, and it is easy to think of S. len (), but this is wrong. It should be len (s ). Because Len () is a built-in function, including in the _ builtin _ module. Python does not include Len () in the string type. At first glance, it seems a bit incomprehensible. In fact, everything has its own logic. Len () can calculate not only the number of characters in a string, but also the number of list members and the number of tuple members. Therefore, it is inappropriate to calculate Len () in a single string, therefore, Len () can be used as a general function, and different types of operations can be implemented with heavy loads. In addition, a Len () can be included in each type of Len () operation () function. Python chooses the first solution. Similarly, the STR (ARG) function represents ARG in the string type.

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. Align ust (width, [fillchar]) # right alignment
S. Center (width, [fillchar]) # center alignment
S. zfill (width) # converts s to width length and alignment right. Fill the remaining 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]) # calculate 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, the deletechars parameter is not supported. You can 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 translation tables.
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. 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. If it is a full number, true is returned; otherwise, false is returned.
S. isspace () # whether it is all blank characters with at least one character
S. islower () # Are all lowercase letters in S?
S. isupper () # Is the uppercase letter in S?
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]) # convert to float

Once again, the string object cannot be changed. That is to say, after creating a string in Python, you cannot change a part of the character. After any of the above functions change the string, a new string is returned, and the original string is not changed. In fact, this is also a work und. You can use the S = List (s) function to change s to a list with a single character as a member, in this way, you can use s [3] = 'A' to change the value, and then use S = "". restore join (s) to a string



1. built-in functions ord and CHR are designed specifically for this purpose:

>>> print ord('a')97>>> print chr(97)a

2. the built-in function ord can also accept Unicode strings with a length of 1. It can return unicode encoded values up to 65536. If you want to obtain a unicode string from the Unicode numeric encoding, you need to use the built-in function unichr:

>>>print ord(u'u2020')8224>>>print repr(unichr(8224))u'\u2020'

3. built-in functions such as ord, CHR, and unichr can convert character or Unicode characters into numbers or vice versa. Note: The difference between CHR (N) and STR (n) is often confusing for beginners:

>>> print repr(chr(97))'a'>>> print repr(str(97))'97'

CHR converts the integers in the parameter and returns the corresponding characters according to ASCII encoding, while STR returns the 10-digit string of this integer.

To convert a string to a list of character encoding values, you can use the built-in functions map and ORD at the same time:

>>> print map(ord, 'ciao')[99, 105, 97, 111]

To convert a list of character encoding values to strings, you need to use '. Join, map, and CHR:

>>> print ''.join(map(chr, range(97, 100)))abc


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: 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.