Five methods for Python string operations

Source: Internet
Author: User

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:

 
 
  1. * S. lower () # lower case
  2. * S. upper () # uppercase
  3. * S. swapcase () # case-insensitive swap
  4. * S. capitalize () # uppercase letters
  5. * String. capwords (S)
  6. # 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 ().
  7. * 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.
  8.  

Alignment of string in output:

 
 
  1. * S. ljust (width, [fillchar])
  2. # 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.
  3. * S. Exactly ust (width, [fillchar]) # right alignment
  4. * S. center (width, [fillchar]) # center alignment
  5. * S. zfill (width) # converts S to width length and alignment on the right. Fill in the missing part with 0.
  6.  

Python string operations also have a pair of encoding and decoding functions:

 
 
  1. * S. encode ([encoding, [errors])
  2. # 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
  3. * S. decode ([encoding, [errors])
  4.  

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

 
 
  1. * S. startwith (prefix [, start [, end])
  2. # Whether to start with prefix
  3. * S. endwith (suffix [, start [, end])
  4. # End with suffix
  5. * S. isalnum ()
  6. # Whether it is all letters and numbers with at least one character
  7. * S. isalpha () # whether it is all letters with at least one character
  8. * S. isdigit () # whether it is all numbers with at least one character
  9. * S. isspace () # whether it is all blank characters with at least one character
  10. * S. islower () # whether all letters in S are in lower case
  11. * S. isupper () # Whether the letters in S are uppercase letters
  12. * S. istitle () # whether S is capitalized
  13.  

Python string type conversion functions, which are only available in the string module:

 
 
  1. * String. atoi (s [, base])
  2. # 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
  3. * String. atol (s [, base]) # convert it to long
  4. * String. atof (s [, base]) # converts it to float

The above is an introduction to Python string operations.

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.