Day9 - 字符串

一、字符串相关方法

1. center、rjust、ljust、zfill(填充)
"""
字符串.center(长度, 填充字符)
字符串.rjust(长度, 填充字符)
字符串.ljust(长度, 填充字符)
字符串.zfill(长度) == 字符串.rjust(长度, ’0‘)
"""
str1 = 'abc'
print(str1.center(7, '1'))          # '11abc11'
print('你好'.center(6, '*'))        # '**你好**'
print('你好'.center(7, '*'))        # '***你好**'
print('hello'.rjust(7, '*'))        # **hello
print('hello'.ljust(7, '*'))        # hello**
print('abc'.zfill(5))                # '00abc'
print('abcdef'.rjust(5, '*'))       # abcdef
2. count(统计次数)
# 字符串1.count(字符串2) -- 统计字符串1只字符串2出现的次数
# 字符串1.count(字符串2, 开始下标, 结束下标) -- 统计开始下标和结束下标确定范围内字符串2的个数
message = 'you see see, one day day!'
print(message.count('a'))           # 2
print(message.count('one'))         # 1
print(message.count('a', 1, 6))     # 0
print(message.count('!', -4))       # 1
3. find、index、rfind、rindex(获取下标)
"""
字符串1.find(字符串2) -- 获取字符串2在字符串1中第一次出现的位置,找不到返回-1
字符串1.find(字符串2, 开始下标, 结束下标)

字符串1.index(字符串2) -- 获取字符串2在字符串1中第一次出现的位置,找不到报错
字符串1.index(字符串2, 开始下标, 结束下标)

rfind和rindex -- 找的时候从后往前找!
"""
message = 'you see see, one day day!'
print(message.find('see'))          # 4
print(message.index('see'))         # 4

print(message.find('abc'))          # 不会报错,返回-1
# print(message.index('abc'))         # 会报错   substring not found

print(message.find('see', -12))     # -1(从下标-12开始往后,找不到’see‘)
print(message.rfind('see'))         # 8
4.is(判断)
"""
字符串.isalnum() -- 判断字符串中是否只包含数字、字母、中文
字符串.isalpha() -- 判断字符串中是否只包含字母和中文
字符串.isdigit() -- 判断字符串是否是纯数字字符串(数字字符)
字符串.isnumeric() -- 判断字符串是否是纯数字字符串(意义是数字的字符)
字符串.isspace() -- 判断字符串是否是纯空白字符串
字符串.isupper() -- 判断字符串中所有的字母是否都是大写字母
字符串.islower() -- 判断字符串中所有的字母是否都是小写字母
"""
print('ab函数s2'.isalnum())       # True
print('asASsd是'.isalpha())      # True
print('2332'.isdigit())         # True
print('百2523十'.isnumeric())     # True
print('\t\n'.isspace())         # True
print('1sAD'.isupper())         # False
print('A'.isupper())            # True
print('a'.islower())            # True
5. join合并(重要)
# 字符串.join(序列) -- 将序列中的元素通过指定的字符串合并成一个新的字符串(序列中的元素必须全部都是字符串)
list1 = ['name', 'age', 'gender']
result = ''.join(list1)
print(result)       # 'nameagegender'

result = '+'.join(list1)
print(result)       # name+age+gender

str1 = 'abc'
result = 'And'.join(str1)
print(result)       # aAndbAndc

# 练习:nums = [100, 200, 300, 400],将nums合并成:’100-200-300-400‘
nums = [100, 200, 300, 400]
result = '-'.join([str(x) for x in nums])
print(result)       # 100-200-300-400

# 练习:已经一个列表,将列表中所有的字符串用'=='连接成一个新的字符串
# list1 = [100, 'abc', True, '你好', 'hello', 12.9]     # 'abc==你好==hello'
list1 = [100, 'abc', True, '你好', 'hello', 12.9]
result = '=='.join([x for x in list1 if type(x) == str])
print(result)           # abc==你好==hello
6. strip、lstrip、rstrip(去空白)
"""
字符串.strip() -- 去掉字符串前后的空白
"""
str1 = '           abc         '
print(str1, ';')
print(str1.strip(), ';', sep='')
print(str1.lstrip(), ';', sep='')
print(str1.rstrip(), ';', sep='')
7. maketrans、translate
"""
str.maketrans(字符串1, 字符串2) -- 创建字符串1和字符串2的对应的表
字符串.translate(表)
"""
str1 = 'hello, how are you!'
table = str.maketrans('ao', '12')     # a-1, o-2
# 'a' -> '1'
result = str1.translate(table)
print(result)

# 0(周一) ~ 6(周天)
# 0-一、1-二、2-三、3-四、4-五、5-六、6-日
table = str.maketrans('0123456', '一二三四五六日')
week = '今天星期1, 明天是星期2, 星期4就放假了, 周5周6出去玩, 最喜欢周4晚上!'
new_week = week.translate(table)
print(new_week)
# 今天星期二, 明天是星期三, 星期五就放假了, 周六周日出去玩, 最喜欢周五晚上!
8. split(切割)
# 字符串1.split(字符串2) -- 将字符串1中所有的字符串2作为切割点对字符串1进行切割
# 字符串1.split(字符串2, N) -- 将字符串1中前N个字符串2作为切割点
str1 = 'mnasdamnsdaamnmnasfamnsada'
result = str1.split('mn')
print(result, type(result))
# ['', 'asda', 'sdaa', '', 'asfa', 'sada'] <class 'list'>
result = str1.split('mn', 3)
print(result)
9. replace替换(重要)
# 字符串1.replace(字符串2, 字符串3) -- 将字符串1中所有的字符串2都替换成字符串3
# 字符串1.replace(字符串2, 字符串3, N) -- 替换前N个
str1 = 'mnasdamnsdaamnmnasfamnsada'
result = str1.replace('mn', '你好')
print(result)

result = str1.replace('mn', '你好', 3)
print(result)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值