数据类型
1.5.1. type 数据类型检测
http://docs.python.org/library/types.html
>>> type( [] ) == list True >>> type( {} ) == dict True >>> type( "" ) == str True >>> type( 0 ) == int True >>> class Test1 ( object ): pass >>> class Test2 ( Test1 ): pass >>> a = Test1() >>> b = Test2() >>> type( a ) == Test1 True >>> type( b ) == Test2 True >>> type( b ) == Test1 False >>> isinstance( b, Test1 ) True >>> isinstance( b, Test2 ) True >>> isinstance( a, Test1 ) True >>> isinstance( a, Test2 ) False >>> isinstance( [], list ) True >>> isinstance( {}, dict ) True
>>> a = [] >>> type(a) <type 'list'> >>> f = () >>> type(f) <type 'tuple'>
1.5.2. 字符串
1.5.2.1. String function
str.find()
找到字符,返回字符串位置。没有找到返回 -1
"aaa bbb ccc".find('aaa')
str.find()
查找并替换字符串
a = 'hello word' a.replace('word','python')
format 方法
DB_URL = 'mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8'.format( USERNAME, PASSWORD, HOST_NAME, PORT, DB_NAME )
格式化字典输出
member = {'name':'neo','age':18} 'my name is {name},age is {age}'.format(**member) #**dict
输出
>>> member = {'name':'neo','age':18} >>> 'my name is {name},age is {age}'.format(**member) 'my name is neo,age is 18'
Convert str to bytes in python
>>> b = str.encode(y) >>> type(b) >>> b b’Hello World!’ To alter from bytes to str, capitalize on bytes.decode(). >>> z = b”Hello World!” >>> y = “Hello World!” >>> type(z) >>> type(y) To alter from str to bytes, capitalize on str.encode(). >>> a = bytes.decode(z) >>> type(a) >>> a ‘Hello World!’ # to utf-8 'BG7NYT'.encode('utf-8') # to utf-16 'BG7NYT'.encode('utf-16')