3.字符串功能实现

# ##################1. upper/ lower 简单用法###################
'''
value = 'alex sb'
print(value)

value = 'alex SB'
new_value = value.upper()
print(new_value)
'''
'''并不会改变value的值,只会新生成一个值
必须将后面新生成的值赋予给一个新变量接受'''


'''
value = 'alex SB'
new_value = value.lower()
print(new_value)
'''


# ######验证码示例
# 繁琐版
'''
check_code = 'iyUF'
message = '请输入验证码 %s :'%(check_code,) # 格式化字符
code = input(message)
new_check_code = check_code.lower()
new_code = code.lower()
if new_check_code == new_code:
print('登陆成功!')
'''

# 简单版
'''
check_code = 'iyUF'
code = input('请输入验证码 %s :'%(check_code,))
if code.lower() == check_code.lower(): # 直接进行比较
print('登陆成功!')
'''

# ##################2.isdigit 简单用法###################

print('''欢迎致电10086
1.话费服务
2.流量服务
3.宽带业务''')
service = input('请选择服务:')
judge = service.isdigit()
# 判断用户输入 字符串 是否可以转换成 数字。 # '666' '张三丰'
print(judge) # 返回布尔值
if judge:
service = int(service)
print(service)
else:
print('请输入数字')



# ##################3.去除空白 strip/lstrip/rstrip#################
'''user = input('请输入用户名:') # ' alex '
new_user1 = user.rstrip() # 去除右边空格
new_user2 = user.lstrip() # 去除左边空格
new_user3 = user.strip() # 去除两边空格 注意:不能删除中间部分
print('——>', user, '<——')
print(new_user1)
print(new_user2)
print(new_user3)
'''

# ##############4.替换 replace #####################
'''
message = input('请说话:')
print(message) # 我去你大爷家里玩
content = message.replace('你大爷', '***') # 将你大爷替换为‘***’
content = message.replace('你大爷', '***', 1) # 从左到右数,需要替换几个
print(content)
'''


# ##############5.切割 split / rsplit #####################
'''
message = '小张现在一脸懵逼,因为他昨晚睡得不好,翻来覆去。'
incision = message.split(',') # 根据逗号进行切割。
incision1 = message.split(',', 1) # 从左到右数,找到第一个逗号进行切割。
incision2 = message.rsplit(',') # 从右到左,根据逗号进行切割。
incision3 = message.rsplit(',', 1) # 从右到左,找到第一个逗号进行切割。

print(incision)
print(incision1)
print(incision2)
print(incision3)

# 注意:分割后返回的值为列表
'''

猜你喜欢

转载自www.cnblogs.com/zyg-ayy/p/11070558.html