04 字符串方法
基本
功能 方法 替换 字符串.replace(替换值,替换目标,次数) 大写 字符串.upper() 小写 字符串.lower() 首字母大写 字符串.capitalize() 标题形式 字符串.title()
str1 = 'hello world'
>> > str1. replace( 'l' , 'k' )
'hekko workd'
>> > str1
'hello world'
>> > str1. upper( )
'HELLO WORLD'
>> > str1. lower( )
'hello world'
>> > str1. capitalize( )
'Hello world'
>> > str1. title( )
'Hello World'
切割、拼接与查找
功能 方法 说明 去掉两边空格 .strip() —— lstrip()或rstrip() 切割 str2.split(切割符,次数) 拼接 ‘拼接符’.join(字符串) 查找元素位置 1 字符串.index(查找值,下标开始) 报错:未找到元素 查找元素位置 2 字符串.find(查找值,下标开始) 返回-1:未找到元素
str2 = ' hello world this is python '
>> > str2. strip( )
'hello world this is python'
>> > str2. split( 'o' , 2 )
[ ' hell' , ' w' , 'rld this is python ' ]
>> > str2. split( 'this' )
[ ' hello world ' , ' is python ' ]
>> > 'he' . join( str2. split( 'this' ) )
' hello world he is python '
>> > str2. index( 'l' , 11 )
11
>> > str2. index( 'l' , 100 )
Traceback ( most recent call last) :
File "<pyshell#12>" , line 1 , in < module>
str2. index( 'l' , 100 )
ValueError: substring not found
>> > str2. find( 'l' , 11 )
11
>> > str2. find( 'l' , 100 )
- 1
判断与转义
功能 方法 判断 全是中文或者字母组成 字符串.isalpha() 数字 isdigit() 大写 isupper() 小写 islower() 转义 换行 \n(在print函数里才起作用) 水平制表符 \t 往前退一格 \b(cmd起作用) 系统提示音 \a(cmd起作用) 空格 \0 取消转义 \ 统一取消转义 r’字符串’
判断
>> > 'hello world' . isalpha( )
False
>> > 'helloworld' . isalpha( )
True
>> > '你好世界' . isalpha( )
True
>> > '123' . isdigit( )
True
>> > '123a' . isdigit( )
False
>> > "HELLO WORLD" . isupper( )
True
>> > "HELLOWORLD" . isupper( )
True
>> > "Hello World" . isupper( )
False
>> > "hello world" . islower( )
True
>> > "Hello World" . islower( )
False
>> > "HELLO WORLD" . islower( )
False
>> > "HELLOWORLD" . islower( )
False
转义
>> > print ( 'hello\npython' )
hello
python
>> > print ( 'hello\tpython' )
hello python
>> > print ( 'hello\bpython' )
hellpython
>> > print ( 'hello\apython' )
hellopython
>> > print ( 'hello\0python' )
hello python
>> > print ( 'hello\\npython' )
hello\npython
>> > print ( r'hello\npython' )
hello\npython
字典
功能 方法 说明 定义-1 {键1:值1,键2:值2} 定义-2 dict(键1=值1,键2=值2) 取值 字典[key] 增 字典.setdefault(键,value) 查值1 dic[key] 报错:不存在的键 查值2 dic.get(key) 返回空:不存在 改值1 dic[key] = value 无则增,有则改 改键值2 dic.update({key:value, key:value}) 无则增,有则改 删1 dic.pop(key) 删除对应键的键值对数据 删2 dic.popitem() 后添加先删除 删3 dic.clear() >全部 获取所有的键名 dic.keys() 获取所有的值 dic.values() 获取所有的键值 dic.items()
dic_1 = { 'name' : 'william' , 'age' : '23' , 'country' : 'Indonesia' }
dic_2 = dict ( name= 'william' , age= 23 , country= 'Indonesia' )
>> > dic_1[ "name" ]
'william'
>> > dic_1
{ 'name' : 'william' , 'age' : '23' , 'country' : 'Indonesia' , 'study' : 'xiamen' }
>> > dic_1[ 'name' ]
'william'
>> > dic_1. get( 'name' )
'william'
>> > dic_1[ 'name' ] = 'Lie Pin'
>> > dic_1
{ 'name' : 'Lie Pin' , 'age' : '23' , 'country' : 'Indonesia' , 'study' : 'xiamen' }
>> > dic_1. update( { 'age' : '40' , 'study' : 'indonesia' , 'job' : 'no' } )
>> > dic_1
{ 'name' : 'Lie Pin' , 'age' : '40' , 'country' : 'Indonesia' , 'study' : '
indonesia', ' job': ' no'}
>> > dic_1. pop( 'job' )
'no'
>> > dic_1
{ 'name' : 'Lie Pin' , 'age' : '40' , 'country' : 'Indonesia' , 'study' : 'indonesia' }
>> > dic_1. popitem( )
( 'study' , 'indonesia' )
>> > dic_1
{ 'name' : 'Lie Pin' , 'age' : '40' , 'country' : 'Indonesia' }
>> > dic_1. clear( )
>> > dic_1
{ }
>> > dic_1 = { 'name' : 'william' , 'age' : '23' , 'country' : 'Indonesia' }
>> > dic_1. keys( )
dict_keys( [ 'name' , 'age' , 'country' ] )
>> > dic_1. values( )
dict_values( [ 'william' , '23' , 'Indonesia' ] )
>> > dic_1. items( )
dict_items( [ ( 'name' , 'william' ) , ( 'age' , '23' ) , ( 'country' , 'Indonesia' ) ] )
集合
项目 项目 说明 特征 去重 无序 定义 set(‘字符串’) 运算 交集 并集 差集 增加 单个 集合.add(‘值’) 批量 集合.update({值1,值2,值3}) 删除 随机 集合.pop() 指定 集合.remove(值) 判断 交集? 集合1.isdisjoint(集合2) set2在set1的下级 集合2.issubset(集合1) set1包含set3的值 集合1.issuperseet(集合3)
特征
>> > set1 = { 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 }
>> > set1
{ 1 , 2 , 3 , 4 }
>> > set2 = { 1 , 'a' , 'b' , 2 , 3 }
>> > set2
{ 1 , 2 , 3 , 'b' , 'a' }
运算
set1 = { 1 , 2 , 3 , 4 , 5 , 7 }
set2 = { 1 , 3 , 5 , 7 }
set3 = { 'a' , 'b' }
>> > print ( set1 & set2)
{ 1 , 3 , 5 , 7 }
>> > print ( set1 | set3)
{ 'b' , 1 , 2 , 3 , 4 , 5 , 'a' , 7 }
>> > print ( set1 - set2)
{ 2 , 4 }
增加和删除
set1 = { 1 , 2 , 3 , 4 }
>> > set1. add( 'a' )
>> > set1
{ 1 , 2 , 3 , 4 , 'a' }
set1. update( 'b' , 'c' , 'd' )
>> > set1. update( 'b' , 'c' , 'd' )
>> > set1
{ 'b' , 1 , 2 , 3 , 4 , 'a' , 'd' , 'c' }
>> > set1
{ 'b' , 1 , 2 , 3 , 4 , 'a' , 'd' , 'c' }
>> > set1. pop( )
'b'
>> > set1
{ 1 , 2 , 3 , 4 , 'a' , 'd' , 'c' }
>> > set1
{ 1 , 2 , 3 , 4 , 'a' , 'd' , 'c' }
>> > set1. remove( 'd' )
>> > set1. remove( 'a' )
>> > set1
{ 1 , 2 , 3 , 4 , 'c' }
判断交叉 | 层级
set1 = { 1 , 2 , 3 , 4 , 5 , 6 }
set2 = { 4 , 5 , 6 }
set3 = { 3 , 4 , 5 }
>> > set1. isdisjoint( set2)
False
>> > set2. issubset( set1)
True
>> > set1. issuperset( set2)
True
其他
功能 身份运算符 A是B? is 否(A是B?) is not A在B? in 否(A在B?) not in
注意:python当中存在内存池,范围[-5,256]
>> > a = 300
>> > b = 300
>> > a == b
True
>> > a is b
False
>> > c = 200
>> > d = 200
>> > c == d
True
>> > c is d
True
优先级顺序
运算符 描述 ** 幂运算 +、- 一元运算符(正负号) *、/、% 算术运算符(乘、除、取余) +、- (加减) <、>、<=、>= 比较运算符 ==、!= =、%=、/=、-=、+=、*=、**= 赋值运算符 is、is not 身份运算符 in、not in 成员运算符 not > and > or 逻辑运算符
BONUS
解码和编码
功能 方法 编码 字符串.encode(encoding=‘utf-8’) 解码 字符串.decode(encoding=‘utf-8’)
>> > "蔡汶桦" . encode( encoding= "utf-8" )
b'\xe8\x94\xa1\xe6\xb1\xb6\xe6\xa1\xa6'
>> > "蔡汶桦" . encode( "utf-8" )
b'\xe8\x94\xa1\xe6\xb1\xb6\xe6\xa1\xa6'
>> > "蔡汶桦" . encode( )
b'\xe8\x94\xa1\xe6\xb1\xb6\xe6\xa1\xa6'
>> > b'\xe8\x94\xa1\xe6\xb1\xb6\xe6\xa1\xa6' . decode( )
'蔡汶桦'
>> > b'\xe8\x94\xa1\xe6\xb1\xb6\xe6\xa1\xa6' . decode( encoding= 'UTF-8' )
'蔡汶桦'
散列类型
可变对象 | 不可变对象
发展规律 实例 可变对象 修改>地址不变 list、dict、set 不可变对象 修改>地址变 数值、str、元组
>> > lis1 = [ 1 , 2 , 3 ]
>> > id ( lis1)
2118046143112
>> > lis1. append( 4 )
>> > lis1
[ 1 , 2 , 3 , 4 ]
>> > id ( lis1)
2118046143112
下载:练习题