一、字符串总结与练习
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "DaChao"
# Date: 2017/6/7 # x = "234567ASDsdfghj" #切片和索引
# print(x[2:-2])
# print(x[2]) # x = "hello" #显示字符串长度,注意是从1开始
# print(len(x)) # x = "hello world ASDF" #返回长度为100的字符串,并在右侧填充0
# print(x.zfill(100)) # x = "hello world ASDF" #小写变为大写
# print(x.upper())
# x = "234567ASDsdfghj" #大写变为小写
# print(x.lower())
# x = "234567sdfghj" #判断是否含有至少一个区分大小写的字符,并且这些都是小写
# # print(x.islower()) # x = "hello world" #返回标题化字符串
# print(x.title()) # x = "Hello World" #翻转字符串中的大小写
# print(x.swapcase()) # x = " hello world " #同时执行lstrip和rstrip,删除两边
# print(x.strip()) # x = "hello world" #检测开头或结尾
# print(x.startswith('hea'))
# x = "hello world"
# print(x.endswith('o',0,5)) # x = "234567ASDsd\nfASDghjASD" #以\n行分隔,返回一个包含元素的列表
# print(x.splitlines(True)) # x = "234567ASDsdfASDghjASD" #以A分隔x,并可指定次数
# print(x.split('A',2)) # x = "234567ASDsdfASDghjASD" #替换字符串,并且可指定次数
# print(x.replace('ASD','ABC',1)) # x = "234567ASDsdfghj" #以7为中间符,分割x
# print(x.partition('7')) # x = "234567ASDZzsdfghj" #返回x中最大的字母(小写)
# print((max(x))) # x = "121 234567ASDsdfghj" #截掉x左边的1
# print(x.lstrip('1')) # x = "234567sdfghj" #左对齐,并以*填充剩余数量(20)
# print(x.ljust(20,'*')) # x = "*" #以x为分隔符重新生成y
# y = "abc"
# print(x.join(y)) # x = "Asdf112321 Gh123J" #判断是否首字符为大写,其它为小写
# print(x.istitle())
# x = " " #判断是否只包含空格
# print(x.isspace())
# x = "234567f" #判断是否只包含*数字字符*
# print(x.isnumeric())
# x = "234567" #判断是否全为数字
# print(x.isdigit())
# x = "234567sdfghj" #判断是否全为十进制数字
# print(x.isdecimal())
# x = "234567sdfghj" #判断是否全为字母
# print(x.isalpha())
# x = "234567sdfghj" #判断是否全为字母或数字
# print(x.isalnum()) # x = "hello world" #index同find,但查不到,会返回异常!!!
# print(x.index('a'))
# x = "hello world" #find查找字符串并返回索引值
# print(x.find('d')) # x = "name:{2},age:{1},sex:{0}" #format格式化字符串
# print(x.format('chao','18','male'))
# x = "name:{},age:{},sex:{}"
# print(x.format('chao','18','male')) # x = "hello \tworld" #\t tab符号
# print(x.expandtabs(100)) # x = "hello world" #在指定范围内,返回l的次数
# print(x.count('l',3,10)) # x = "hello world" #中间插入字符串,两边填充*
# print(x.center(30,'*'))
字符串总结及练习
二、作业及相关
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "DaChao"
# Date: 2017/6/7 '''
work8:
1.两层while循环,外层的while循环,让用户输入用户名、密码、工作了几个月、每月的工资(整数),
用户名或密码为空,或者工作的月数不为整数,或者月工资不为整数,则重新输入
2.认证成功,进入下一层while循环,打印命令提示,有查询总工资,查询用户身份
(如果用户名为alex则打印super user,如果用户名为yuanhao或者wupeiqi
则打印normal user,其余情况均打印unkown user),退出功能
3.要求用户输入退出,则退出所有循环(使用tag的方式) 运行效果如下:
user: egon
password: 123
work_mons: 12
salary: 10000 1 查询总工资
2 查询用户身份
3 退出登录 >>: 1
总工资是: 120000.0 1 查询总工资
2 查询用户身份
3 退出登录 >>: 2
unkown user 1 查询总工资
2 查询用户身份
3 退出登录 >>: 3
''' #work8 2
tag = True while tag:
while tag:
user = input("Please input your username: ")#解决问题:用户名必须非空格和回车!!!
if len(user) != 0 and not user.isspace():
break
while tag:
password = input("Please input your password: ")
if len(password) != 0 and not password.isspace():
break
workhours = input("Please input your work hours: ")
salary_m = input("Please input your monthly salary: ")
tag = workhours.isdigit() and salary_m.isdigit()
while tag:
print(" 1、查询总工资")
print(" 2、查询用户身份")
print(" 3、退出登录")
order = input("Please input your choose number: ")
# while order == "1" or "2" or "3":
if order == "": #如果不按照惯性输入,怎么办??
print("总工资是: ",int(workhours)*int(salary_m)) #对输入数字取整数!!!
elif order == "":
if user == "alex":
print("*******Super user*******")
elif user == "yuanhao" or "wupeiqi":
print("*******Normal user*******")
else:
print("*******Unknown user*******")
elif order == "":
print("*******用户已退出!*******")
tag = False #work8 1
# tag = True
#
# while tag:
# user = input("Please input your username: ") #有个问题:直接回车也跳过指令!!!
# password = input("Please input your password: ")
# jobtime = input("Please input your time of job: ")
# salary_m = input("Please input your monthly salary: ")
# tag = user.isspace() or password.isspace() or not jobtime.isdigit() or not salary_m.isdigit()
# while not tag:
# print(" 1、查询总工资")
# print(" 2、查询用户身份")
# print(" 3、退出登录")
# order = input("Please input your choose number: ")
# if order == "1":
# print("总工资是: ",int(jobtime)*int(salary_m))
# elif order == "2":
# if user == "alex":
# print("Super user")
# elif user == "yuanhao" or "wupeiqi":
# print("Normal user")
# else:
# print("Unknown user")
# elif order == "3":
# print("用户已退出!")
# break '''
work7: 编写while循环,让用户输入内容,判断输入的内容以alex开头的,则将该字符串加上_SB结尾
''' # tag = True
# while tag:
# content = input("Please input your content: ")
# tag = not content.startswith("alex")
# print(content,"SB") '''
work6: 编写while循环,让用户输入用户名和密码,如果用户为空或者数字,则重新输入
''' # tag = True
# while tag:
# user = input("Please input your username: ")
# password = input("Please input your password: ")
# tag = user.isspace() or user.isdigit()
# print("You passed!") '''
work5: 编写while循环,要求用户输入命令,如果命令为空,则继续输入
''' # tag = True
# while tag:
# order = input("Please input your order: ")
# tag = order.isspace()
# print("You passed.") '''
work4: msg='/etc/a.txt|365|get' 将该字符的文件名,文件大小,操作方法切割出来
''' # msg = '/etc/a.txt|365|get'
# print(msg.split('|')) '''
work3: msg='hello alex'中的alex替换成SB
''' # msg = 'hello alex'
# print(msg.replace("alex","SB")) '''
work2: 编写while循环,利用索引遍历出每一个字符
''' # w = "My daughter has some cartoon characters on her shirt."
# i = 0
# while i < len(w):
# print (w[i])
# i+=1 '''
work1:编写for循环,利用索引遍历出每一个字符
''' # w = "My daughter has some cartoon characters on her shirt."
#
# for i in range(0,len(w)):
# print(w[i])
作业及相关