Two decoding and decoding functions in a Python string

Source: Internet
Author: User

Python strings are widely used in computer languages. Few people know that Python strings contain two encoding and decoding functions. The following is an introduction to the article. I hope you will learn from the following articles. The following is an introduction to this article.

Python string, which contains two functions:

 
 
  1. String.maketrans(from, to)   

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

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

Python strings also have a pair of encoding and decoding functions:

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

 
 
  1. 'ignore', 'replace',   
  2. 'xmlcharrefreplace',   
  3. 'backslashreplace'  

And all values registered through codecs. register_error. This part of content involves the codecs module, not the plain white

 
 
  1. S.decode([encoding,[errors]])   

Test functions of Python strings, which are not in the string module. These functions return bool values:

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

# 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 small S. isupper () # Whether the letters in S are uppercase S. whether istitle () # S is capitalized.

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.