Today, we mainly explain the commonly used methods of int, float, str, list, tuple, set class. The difference has been encode (encoded) and decode (decoding).
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/75/59/wKioL1Y3XKbi3wQLAAGVrT3yed4423.jpg "title=" QQ picture 20151102205008.png "alt=" Wkiol1y3xkbi3wqlaagvrt3yed4423.jpg "/>
As shown, regardless of the style of the character set of the operating system itself. When they communicate with each other, the character set is converted to
Unicode this universal code. So there is no barrier between their communication.
For example, to convert the Chinese character ' king ' of the Utf-8 character set to GBK format, you need to do the following
#先获取 ' King ' of the UTF-8 code
King
' \xe7\x8e\x8b '
#调用decode解码成unicode编码, with U ' is Unicode encoding
x= ' \xe7\x8e\x8b ' x.decode (' UTF8 ') u ' \u738b '
#最后调用encode方法将Unicode编码编译成gbk格式编码
X.decode (' UTF8 '). Encode (' GBK ') ' \xcd\xf5 '
If this encoding is printed due to the operating system default character set, it may appear garbled
Y=x.decode (' UTF8 '). Encode (' GBK ') print y
The common methods of integer int classes are
#将输入转换为整数型
X=int (Ten) [Object Object]
#将输入转换为二进制数, where 0b represents a binary number
X=bin () Print x0b1010
#将输入转换成8进制数
X=oct () Print x10
#将输入转换成16进制数
X=hex (one) print x0xb
#将输入取绝对值
X=abs ( -9) Print x9
#比较两个数的大小, returns 1 if the first number is less than the second number, and returns 0 if equal, greater than 1
X=9CMP (x,10) -1cmp (x,9) 0cmp (x,8) 1
#将两个数相除, seeking quotient and remainder. For example 9 divided by 2 quotient 4 more than 1
Divmod (9,2)
(4, 1)
Float Float class The method of long integer is similar to the integer type
Only the floating-point type is the number of decimal places, such as 3.14,9.81.
Long integers are processed with long integer numbers such as: 123123189788
Common methods of String str class
#首字母大写
x= ' abc ' x.x.capitalize () ' ABC '
#内容居中, Width: total length; Fillchar: padding content in white space, default None
x= ' abc ' X.center (Ten) ' ABC ' X.center (' * ') ' ***abc**** '
#内容左侧对齐, Width: total length; Fillchar: padding content in white space, default None
x= ' abc ' X.ljust (Ten, ' # ') ' abc####### '
#子序列个数
x= ' DASDASD ' X.count (' d ') 3
#判断字符串是否为数字或者字母组成
x= ' abc123 ' x.isalnum () truey= ' 21daq*# ' X.isalnum () False
#判断字符串是否为字母
x= ' abc ' X.isalpha () True
#判断字符串是否为数字
x= ' 123 ' X.isnum () True
#删除字符串前面和后面的空格, line breaks, and other symbols
x= ' da sda\n ' X.strip () ' Da SDA '
#以指定的字符将原有的字符串分为前, middle and back three segments
x= ' DASDASDASDA ' x.partition (' s ') (' da ', ' s ', ' DASDASDA ')
#替换字符串中的内容
x= ' DASD ' x.replace (' s ', ' 123 ') ' da123d '
#以指定字符的内容字符串为列表
x= ' DADASD ' x.split (' a ') [' d ', ' d ', ' SD ']
#将字符串中的大小写互换
x= ' abc ' x.swapcase () ' ABC '
#将字符串内容全部转换成小写
x= ' abc ' x.lower () ' ABC '
#将字符串内容全部转换成小写
x= ' abc ' x.upper () ' ABC '
#取字符串长度
x= ' Dasdasdasdads ' len (x) 13
This article from "Thunderbolt Tofu" blog, declined reprint!
Python Training Day2 Essays