第九天 字符串
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(10, '%')) # '%%%%%hello'
print('hello'.ljust(10, '%')) # 'hello%%%%%'
print('abc'.zfill(5)) # '00abc'
2. count
字符串1.count(字符串2) - 统计字符串1中字符串2出现的次数
字符串1.count(字符串2, 开始下标, 结束下标) - 统计开始下标和结束下标确定范围内字符串2的个数
message = 'you see see, one day day!'
print(message.count('a'))
print(message.count('one'))
print(message.count('a', 1, 6))
print(message.count('a', -4))
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')) # 报错!ValueError: substring not found
print(message.find('see', -12)) # -1 (从下标为-12开始往后,找不到'see')
print(message.rfind('see')) # 8
4.特殊符号
字符串.isalnum() - 判断字符串中是否只包含数字、字母、中文
字符串.isalpha() - 判断字符串中是否只包含字母和中文
字符串.isdigit() - 判断字符串是否是纯数字字符串(数字字符)
字符串.isnumeric() - 判断字符串是否是纯数字字符串(意义是数字的字符)
字符串.isspace() - 判断字符串是否是纯空白字符串
字符串.isupper() - 判断字符串中所有的字母是否都是大写字母
字符串.islower() - 判断字符串中所有的字母是否都是小写字母
print('ab函数s2'.isalnum())
print('abASsjh是'.isalpha())
print('2332'.isdigit())
print('百2Ⅴ3拾32一十贰'.isnumeric())
print(' \t\n'.isspace())
print('23JAJD'.isupper())
print('P'.isupper())
print('r'.islower())
5. join(重要)
字符串.join(序列) - 将序列中的元素通过指定的字符串合并成一个新的字符串(序列中的元素必须全部都是字符串)
list1 = ['name', 'age', 'gender']
# 'nameagegender'
result = ''.join(list1)
print(result) # 'nameagegender'
# 'name+age+gender'
result = '+'.join(list1)
print(result) # name+age+gender
str1 = 'abc'
# 'aAndbAndC'
result = 'And'.join(str1)
print(result) # 'aAndbAndc'
# 练习:nums = [100, 200, 300, 400], 将nums合并成:'100-200-300-400'
nums = [100, 200, 300, 400] # ['100', '200', '300', '400']
result = '-'.join([str(x) for x in nums])
print(result)
# 练习:已经一个列表,将列表中所有的字符串用'=='连接成一个新的字符串
list1 = [100, 'abc', True, '你好', 'hello', 12.9] # 'abc==你好==hello'
# ['abc', '你好', 'hello']
result = '=='.join([x for x in list1 if type(x) == str])
print(result)
6. strip、lstrip、rstrip
字符串.strip() - 去掉字符串前后的空白
str1 = ' abc 123 '
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! abc'
table = str.maketrans('ao', '12') # a-1,o-2
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.split(字符串2, N) - 将字符串1中前N个字符串2作为切割点
str1 = 'abcMN123MNxyzMN=-'
result = str1.split('MN')
print(result) # ['abc', '123', 'xyz', '=-']
str1 = 'MNabcMN123MNMNxyzMN=-MN'
print(str1.split('MN')) # ['', 'abc', '123', '', 'xyz', '=-', '']
str1 = 'abcMN123MNxyzMN=-'
result = str1.split('MN', 2)
print(result)
9. replace(重要)
字符串1.replace(字符串2, 字符串3) - 将字符串1中所有的字符串2都替换成字符串3
字符串1.replace(字符串2, 字符串3, N)
str1 = 'MNabcMN123MNMNxyzMN=-MN'
result = str1.replace('MN', '你好')
print(result)
result = str1.replace('MN', '你好', 2)
print(result) # 你好abc你好123MNMNxyzMN=-MN