字符串操作
字符串数字类型,不可以被改变
1.1.1、索引取值(正向取+反向取) :只能取,不能改
msg = 'hello world'
# 正向取
print(msg[0])
print(msg[5])
# 反向取
print(msg[-1])
# 只能取,不能改
msg[0]='H'
1.1.2、 切片:索引的拓展应用,从一个大的字符串中拷贝出一个子字符串(顾头不顾尾,步长)
msg='hello world'
# 顾头不顾尾
res = msg[0:5]
print(res)
print(msg)
1.1.3、成员运算in和not in
判断一个子字符串是否存在于一个大字符串中
1.1.4、移除字符串左右两侧的符号strip
# 默认去掉的空格
msg=' enon '
res= msg.strip()
print(msg) # 不会改变原值
print(res) # 是产生新值
# 默认去掉左右两侧的*
msg='****enge****'
print(msg.strip('*'))
# 了解: strip只取两边,不去中间
msg='*e***enge****'
print(msg.strip('*'))
msg='*/*=-***enge*-==*()**'
print(msg.strip('*/=-()'))
应用
inp_user=input('your name>>: ').strip()
inp_pwd=input('your password>>; ').strip()
if inp_user == 'egon' and inp_pwd == '123':
print('登录成功')
else:
print('账号密码错误')
1.5、切分sqlit: 把一个字符串按照某种分隔符进行切分,得到一个列表
# 默认分隔符是空格
info='egon 18 male'
res = info.split()
print(res)
# ['egon', '18', 'male']
# 指定分隔符
info='egon:18:male'
res = info.split(':')
print(res)
# 指定次数
info='egon:18:male'
res = info.split(':',1)
print(res)
# ['egon', '18:male']
1.6、循环
info='egon:18:male'
for x in info:
print(x)
需要掌握
1.2.1、strip,lstrip,rstrip
msg = '*******egon****'
print(msg.strip('*')) # 去除两边
print(msg.lstrip('*')) # 只去左边
print(msg.rstrip('*')) # 只去右边
1.2.2、lower,upper
msg = 'AAbbccC'
print(msg.lower()) # 全小写
print(msg.upper()) # 全大写
1.2.3、staryswith,endswith
print("alex is sb".startswith("alex")) # 以什么开头
print("alex is sb".endswith('sb')) # 以什么结尾
1.2.4、split,rsplit : 将字符串切成列表
info ='egon:18:male'
print(info.split(':',1)) # ["egon","18:male"]
print(info.rsplit(':',1)) # ["egon:18","male"]
1.2.5、join:把列表拼接成字符串
l=['egon', '18', 'male']
# res = l[0]+":"+l[1]+":"+l[2]
res = ":".join(l) # 按照某个分隔符符号,把元素全为字符串的列表拼接成一个大字符串
print(res)
1.2.6、replace: 替换
msg = "you can you up no can no bb"
print(msg.replace("you","You")
print(msg.replace("you","You",1))
1.2.7、isdigit
# 判断字符串是否由纯数字组成
print('123'.isdigit())
print('1s.23'.isdigit())
示例:
age = input('请输入你的年龄: ').strip() # 清除空格
if age.isdigit(): # 判断是否由数字组成
age = int(age) # int("afdjjhk")
if age > 18:
print("猜大了")
elif age < 18:
print("猜小了")
else:
print("猜对了")
else:
print('必须输入数字')
了解
1.3.1、find,rfind,index,rindex,count
msg="hello egon hahahah"
# 找到返回起始索引
print(msg.find('e')) # 返回要查找的字符串在大字符串中的起始索引
print(msg.find('egon'))
print(msg.index('e'))
print(msg.index('egon'))
# 区别在于找不到
print(msg.find('xxx')) # 返回-1,代表找不到
print(msg.index('xxx')) # 抛出异常
msg="hello egon hahahah egon hahahah egon hahahah"
print(msg.count('egon'))
# 1.3.2、center,ljust,rjust,zfill
print('egon'.center(50,'*'))
print('egon'.ljust(50,'*')) # 左对齐
print('egon'.rjust(50,'*')) # 右对齐
print('egon'.zfill(10)) # 默认左对齐
# 1.3.3、expandtabs
msg = 'hello\tworld'
print(msg.expandtabs(2)) # 设置制表符代表的空格数位2
# 1.3.4、capta;ize,swapcase,title
print("hello world egon".capitalize()) # 字符串首字母大写
print("hello world egon".swapcase()) # 大小写反转
print("hello world egon".title()) # 每个字符首字母大写
# 1.3.5、is数字系列
# 1.3.6、is其他
print('abC'.islower())
print('ABC'.isupper())
print('Hello World'.istitle()) # 每个单词的首字母大写结果为True
print('123sad'.isalnum()) # 数或字母组成结果为True
print('fghj'.isalpha()) # 由字母组成结果为True
print(' '.isspace()) # 字符串由空格组成结果为True
print('if'.isidentifier()) # 内置