字符串功能
今日内容
- 整形
- 布尔类型
- 字符串
内容回顾和补充
内容回顾
思维导图罗列知识点
- xmind软件
Python添加注释
补充
-
运算符补充
-
in
-
value = "我是中国人" # 判断‘中国’是否在value所指代的字符串中,“中国”是否是value所指代的字符串的子序列 v1="中国" in value 实例: content = input('请输入内容:') if '退钱' in content: print('包含敏感字符') 实例: while True: content = input('请输入内容:') if '退钱' in content: print('包含敏感字符') else: print(content) break
-
-
not in
-
-
优先级
not 2 > 1 not 2 > 1 #错误 not 2>1 #正确
程序逻辑:
- 功能拆分
- 拼凑功能
- 中文理清思路
用户三次登录
#功能1
user = input('请输入用户名:')
pwd = input('请输入密码')
if user == 'xiaoming' and pwd == '123.com'
print('登陆成功')
else:
print('登录失败')
# 功能二:三次机会
n=0
while n<3:
print(n)
n += 1
###功能嵌套####
n=1
while n<= 3:
print(n)
user = input('请输入用户名:')
pwd = input('请输入密码')
if user == 'xiaoming' and pwd == '123.com':
print('登录成功')
else:
print('登录失败')
if n == 3:
choice = input('请输入是否继续(Y/N):')
if choice == 'N':
break
elif choice == 'Y':
co
continue
n += 1
# 允许用户登录3次,3次还没成功,用户还想登录输入y,就可以在登录3次,如此反复
content = 1
while content <=3:
print(content)
user = input('请输入姓名:')
password = input('请输入密码:')
if user == 'xiaoming' and password == '123.com':
print('登录成功')
break
else:
print('登陆失败')
sum = input('是否继续(Y/N):')
if sum == 'N':
break
elif sum == 'Y':
content = 1
continue
else:
print('输入错误')
break
content += 1
#用户登录3次并提示剩余次数
content = 1
while content <=3:
user = input('请输入姓名:')
password = input('请输入密码:')
if user == 'xiaoming' and password == '123.com':
print('登录成功')
break
else:
timer = 3 - content
print("输入用户或密码错误,剩余机会%s" % (timer))
content += 1
#方法二
content = 2
while content >=0:
user = input('请输入姓名:')
password = input('请输入密码:')
if user == 'xiaoming' and password == '123.com':
print('登录成功')
break
else:
print("剩余机会",content)
content -= 1
简洁:
content = 2
while content >=0:
user = input('请输入姓名:')
password = input('请输入密码:')
if user == 'xiaoming' and password == '123.com':
print('登录成功')
break
print("剩余机会",content)
content -= 1
else:
print('三次机会用完')
内容详细
1.整形(int)
age = 18
-
py2
-
int
-
32位电脑:范围自己查
-
64位电脑:范围自己查
-
超出范围后Python自动将其转换成long(长整形)
-
整数除法只能保留整数位
#得到正确值 from __future__ import division v = 9/2 print(v)
-
-
py3
- int
- 整形除法只能保留所有
2.布尔值(bool/boolen)
- 只有两个值:True/False
- 转换
- 数字转布尔:0是False,其他都是True
- 字符串转布尔:""是False,其他都是True
3.字符串(str/string)
-
字符转换大小写
value = 'xiaoming' new_value = value.upper() #大写 print(value,new_value) value = 'xiaomING' new_value = value.lower() #小写 print(value,new_value)
验证码示例: check_code = 'iyUF' message = '请输入验证码%s:'%(check_code,) code = input(message) new_check_code = check_code.lower() new_code = code.lower() if new_code == new_check_code: print('输入成功') 改造示例: check_code = 'iyUF' code = input('请输入验证码%s:'%(check_code,)) if code.lower() == check_code.lower(): print('登录成功')
-
isdigit 判断字符串是否为数字
######isdigit(返回布尔值)##### print('''欢迎致电10086 1.话费查询 2.业务办理 3.款单 ''') while True: num = input('请选择服务:') #判断用户输入 字符串 是否可以转换成数字 flag = num.isdigit() #print(flag) #'1' -> True 'asdf' -> False if flag: num = int(num) else: print('请输入数字')
-
去掉输入值空格 strip/rstrip/lstrip
user = input('请输入用户名:') new_user1 = user.rstrip() #去掉右边空格 new_user2 = new_user1.lstrip() #去掉左边空格 data = new_user2.upper() print('-->',new_user2,'<---') user = input('请输入用户名:') new_user = user.strip() #去掉两边空格
-
替换字符 replace
message = input('请说话:') print(message) #我去你大爷的家玩 data = message.replace('大爷','**') #替换所有'大爷' data = message.replace('大爷','**',1) #从左到右替换第一个'大爷',如果是2就替换前两个'大爷' print(data)
-
切割 split
message = '小黑一脸懵逼,因为昨天晚上一直在学习,指导深夜' result = message.split(',') #逗号是中文逗号因为原句是中文逗号 result = message.split(',',1) #从左到右找到第一个逗号切割 #result = message.rsplit(',',1) #从左到右切割 print(result)
总结:
-
字符串特有
- .upper() / .lower()
- .isdigit()
- .strip() / .lstrip() / .rstrip()
- .replace(“被替换的字符/子序列”,“要替换为的内容”) / .replace(“被替换的字符/子序列”,“要替换为的内容”,1)
- .split(‘根据什么东西进行分割’) / .split(‘根据什么东西进行分割’,1) / .rsplit
-
公共
-
len,计算长度。(字符串->计算字符串中的字符个数)
计算长度 value = 'xiaoming' number = len(value) print(number) #索引 value = 'xiaoming' data = value[2] print(data) ########### value = 'adcssdkjha' value_len = len(value) index = 0 while True: val = value[index] print(val) if index == value_len-1: break index += 1
-
-
练习
示例: 让用户输入任意字符串,获取字符串之后计算其中有多少个数字 text =