Python strings are widely used in computer languages and we will encounter many difficulties in the application process. The following article describes how to operate Python strings, I hope you will find some gains after reading the following articles. The following is a detailed introduction to the Python string operation application.
Case-insensitive conversion of characters in Python strings:
- * 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 using the split () function,
Then, use capitalize () to convert the first letter to uppercase, and then merge it 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.
-
Python string operations also have 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
-
Python string type conversion functions, which 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
The above is an introduction to Python string operations.