Python training Day2

Source: Internet
Author: User

Python training Day2
Today, we mainly explain the common methods of int, float, str, list, tuple, and set classes. The difference between encode and decode. Regardless of the operating system character set. When they communicate with each other, they all need to convert the character set to Unicode, such as the universal code. In this way, there will be no barriers to communication between them. For example, to convert the UTF-8 character set 'King' into GBK format, you need the following Operation # first obtain the 'King' UTF-8 encoding 'King' \ xe7 \ x8e \ x8b '# Call decode is decoded to unicode encoding, the Unicode encoding x = '\ xe7 \ x8e \ x8b' x. decode ('utf8') U' \ u738b' # Call The encode method to compile Unicode encoding into gbk format encoding x. decode ('utf8 '). encode ('gbk') '\ xcd \ xf5'. If this encoding is printed, garbled characters may occur due to the default Character Set of the operating system. decode ('utf8 '). encode ('gbk') print y integer int common methods include # convert input to integer x = int (10) [object Object] # convert input to binary number, where 0 B indicates the binary number x = bin (10) print x0b1010 # convert the input to an octal number x = oct (10) print x10 # convert the input to hexadecimal number x = hex (11) print x0xb # obtain the absolute value of the input x = abs (-9) print x9 # compare the two numbers. If the first number is smaller than the second number,-1 is returned. If it is equal, 0 is returned. If it is greater than, 1 x = 9cmp (x, 10) is returned) -1cmp (x, 9) 0cmp (x, 8) 1 # divide two numbers, and calculate the quotient and remainder. For example, 9 divided by 2 vendors, 4 more than 1 divmod (9, 2) (4, 1) float methods the long integer long method is similar to the integer type, except that the floating point type is used to process long integers with decimal points, such as 3.14 and 9.81: 123123189788 common methods for string str classes # uppercase x = 'abc' x. x. capitalize () 'abc' # center content, width: total length; fillchar: blank space filled content, NO x = 'abc' x by default. center (10) 'abc' x. center (10, '*') '***** abc *****' # Align content on the left side, width: total length; fillchar: blank space filled with content, NO x = 'abc' x by default. ljust (10, '#') 'abc ####### '# Number of subsequences x = 'dasdasd' x. count ('D') # determine whether the string is composed of digits or letters x = 'abc123' x. isalnum () Truey = '21daq * # 'x. isalnum () False # determine whether the string is a letter x = 'abc' x. isalpha () True # determine whether the string is a number x = '000000' x. isnum () True # Delete spaces before and after a string, line breaks, and other symbols x = 'da sda \ n' x. strip () 'da sda' # divides the original string into the first, middle, and last three paragraphs x = 'dasdasdasda' x. partition ('s') ('da', 's', 'dasdasda') # Replace the content in the string x = 'dasd' x. replace ('s', '123') 'da123d '# list x = 'dadase' x with the specified character content string. split ('A') ['D', 'D', 'sd'] # swap the case in the string x = 'abc' x. swapcase () 'abc' # convert all string content to lowercase x = 'abc' x. lower () 'abc' # convert all string content to lowercase x = 'abc' x. upper () 'abc' # String Length x = 'dasdasdasdads' len (x) 13

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.