Python基础(一)——字符串、列表和元组的常用方法

1 字符串

find:从左边开始找第一个字符出现的下标位,找不到返回-1

rfind:从右边开始找

index:与find相似,不同点在找不到时会报错

rindex:从右边开始找

count:统计某个字符在字符串中出现的次数

replace:替换指定的字符,默认为全局替换

split:以某个字符或字符串为分隔符进行切片,分隔符被忽略掉

partition:以某个字符或字符串为分隔符进行切片,分隔符也作为片段

capitalize:将字符串的第一个字符大写

casefold:将字符串的所有字符改为小写

lower:将字符串中的每个大写字母改为小写

upper:将字符串中的每个字母改为大写

title:将字符串中每个单词首字母大写

startswith:判断是否以某个字符串开头

endswith:判断是否以某个字符串结尾

ljust:返回一个字符串左对齐,使用空格填充至长度width的新字符串

rjust:返回一个字符串右对齐,使用空格填充至长度width的新字符串

center:返回一个字符居中对齐,使用空格填充至长度width的新字符串

strip:删除字符串左右两边的空白字符

lstrip:删除字符串左边的空白字符

rstrip:删除字符串右边的空白字符

splitlines:按照换行符分隔字符串,返回一个包含各行作为元素的列表

isalpha:判断字符串是否所有字符都是字母

isdigit:判断字符串是否所有字符都是数字

isalnum:判断字符串是否所有字符都是字母或者数字

isspace:判断字符串是否只包含空格

join:字符串中每个字符插入list的每两个元素中间,构成新的字符串

translate:处理单个字符,替换字符串中某些部分

string='   Hello, world    '
print(string.find('Hello'))
print(string.rfind('Hello'))
print(string.count('o'))
new_string=string.replace('l','L')
print(new_string)
print(string.title())
print(string.strip())
print(string.split())
lis=['apple','banana','orange']
print('-'.join(lis))
print(new_string.translate(str.maketrans(' ','m')))
3
3
2
   HeLLo, worLd    
   Hello, World    
Hello, world
['Hello,', 'world']
apple-banana-orange
mmmHeLLo,mworLdmmmm

2 列表

2.1 添加元素

append:在列表后追加元素

extend:将另一个集合里的元素逐一添加到列表后面

insert:将元素插入到列表的指定位置

test_list=[1,2,3,'a','b']
extend_list=[4,5,6,7]
test_list.append('c')
print('append结果:',test_list)
test_list.extend(extend_list)
print('extend结果:',test_list)
test_list.insert(2,'A')
print('insert结果:',test_list)
append结果: [1, 2, 3, 'a', 'b', 'c']
extend结果: [1, 2, 3, 'a', 'b', 'c', 4, 5, 6, 7]
insert结果: [1, 2, 'A', 3, 'a', 'b', 'c', 4, 5, 6, 7]

2.2 删除元素

pop:弹出列表的最后一个元素

del:删除列表指定索引的元素

remove:删除列表指定元素

test_list=[1, 2, 'A', 3, 'a', 'b', 'c', 4, 5, 6, 7]
test_list.pop()
print('pop结果:',test_list)
del test_list[0]
print('del结果:',test_list)
test_list.remove('b')
print('remove结果:',test_list)
pop结果: [1, 2, 'A', 3, 'a', 'b', 'c', 4, 5, 6]
del结果: [2, 'A', 3, 'a', 'b', 'c', 4, 5, 6]
remove结果: [2, 'A', 3, 'a', 'c', 4, 5, 6]

2.3 查找元素

in/not in:元素存在返回True/False,否则相反

index:查找元素在列表中的索引

count:统计元素在列表中出现的次数

test_list=[1,1,1,2,2,2,3,3,3,4,5,7]
print('1在列表中:',1 in test_list)
print('6不在列表中:',6 not in test_list)
print('1在列表中的索引:',test_list.index(1))
print('1在列表中出现的次数:',test_list.count(1))
1在列表中: True
6不在列表中: True
1在列表中的索引: 0
1在列表中出现的次数: 3

2.4 修改元素

列表是可变类型,所以可以直接通过修改对应索引的元素的值来修改列表

test_list=[1,2,3,'a',5,6]
test_list[3]=4
print(test_list)
[1, 2, 3, 4, 5, 6]

2.5 其他常用方法

sort:将列表进行排序,默认按照从小到大排序,通过参数可以修改排序方式

reverse:逆置列表

enumerate:枚举列表

test_list=[3,2,5,1,6,4]
test_list.reverse()
print('逆置之后的列表 ',test_list)
test_list.sort()
print('正常排序的列表 ',test_list)
test_list.sort(reverse=True)
print('逆向排序的列表 ',test_list)
print({indx:character for indx,character in enumerate(test_list)})
逆置之后的列表  [4, 6, 1, 5, 2, 3]
正常排序的列表  [1, 2, 3, 4, 5, 6]
逆向排序的列表  [6, 5, 4, 3, 2, 1]
{0: 6, 1: 5, 2: 4, 3: 3, 4: 2, 5: 1}

3 元组

由于元组是只能读不能写的,所以常见方法为:

index:查找元素在元组中的索引

count:统计元素在元组中出现的次数

另外,元组也是通过索引访问的,读元组的方法同列表。

test_tuple=(1,2,3,1,4,5)
print(test_tuple.index(1))
print(test_tuple.count(1))
0
2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值