Day 14 python 之 字符串练习

一、字符串总结与练习

 1 #! /usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 # __author__ = "DaChao"
 4 # Date: 2017/6/7
 5 
 6 # x = "234567ASDsdfghj"  #切片和索引
 7 # print(x[2:-2])
 8 # print(x[2])
 9 
10 # x = "hello"     #显示字符串长度,注意是从1开始
11 # print(len(x))
12 
13 # x = "hello world ASDF"  #返回长度为100的字符串,并在右侧填充0
14 # print(x.zfill(100))
15 
16 # x = "hello world ASDF"  #小写变为大写
17 # print(x.upper())
18 # x = "234567ASDsdfghj"  #大写变为小写
19 # print(x.lower())
20 # x = "234567sdfghj"  #判断是否含有至少一个区分大小写的字符,并且这些都是小写
21 # # print(x.islower())
22 
23 # x = "hello world"   #返回标题化字符串
24 # print(x.title())
25 
26 # x = "Hello World"   #翻转字符串中的大小写
27 # print(x.swapcase())
28 
29 # x = "     hello world     "     #同时执行lstrip和rstrip,删除两边
30 # print(x.strip())
31 
32 # x = "hello world"   #检测开头或结尾
33 # print(x.startswith('hea'))
34 # x = "hello world"
35 # print(x.endswith('o',0,5))
36 
37 # x = "234567ASDsd\nfASDghjASD"  #以\n行分隔,返回一个包含元素的列表
38 # print(x.splitlines(True))
39 
40 # x = "234567ASDsdfASDghjASD"  #以A分隔x,并可指定次数
41 # print(x.split('A',2))
42 
43 # x = "234567ASDsdfASDghjASD"  #替换字符串,并且可指定次数
44 # print(x.replace('ASD','ABC',1))
45 
46 # x = "234567ASDsdfghj"  #以7为中间符,分割x
47 # print(x.partition('7'))
48 
49 # x = "234567ASDZzsdfghj"  #返回x中最大的字母(小写)
50 # print((max(x)))
51 
52 # x = "121   234567ASDsdfghj"  #截掉x左边的1
53 # print(x.lstrip('1'))
54 
55 # x = "234567sdfghj"  #左对齐,并以*填充剩余数量(20)
56 # print(x.ljust(20,'*'))
57 
58 # x = "*"  #以x为分隔符重新生成y
59 # y = "abc"
60 # print(x.join(y))
61 
62 # x = "Asdf112321 Gh123J"  #判断是否首字符为大写,其它为小写
63 # print(x.istitle())
64 # x = "  "  #判断是否只包含空格
65 # print(x.isspace())
66 # x = "234567f"  #判断是否只包含*数字字符*
67 # print(x.isnumeric())
68 # x = "234567"  #判断是否全为数字
69 # print(x.isdigit())
70 # x = "234567sdfghj"  #判断是否全为十进制数字
71 # print(x.isdecimal())
72 # x = "234567sdfghj"  #判断是否全为字母
73 # print(x.isalpha())
74 # x = "234567sdfghj"  #判断是否全为字母或数字
75 # print(x.isalnum())
76 
77 # x = "hello world"   #index同find,但查不到,会返回异常!!!
78 # print(x.index('a'))
79 # x = "hello world"   #find查找字符串并返回索引值
80 # print(x.find('d'))
81 
82 # x = "name:{2},age:{1},sex:{0}"  #format格式化字符串
83 # print(x.format('chao','18','male'))
84 # x = "name:{},age:{},sex:{}"
85 # print(x.format('chao','18','male'))
86 
87 # x = "hello \tworld"   #\t tab符号
88 # print(x.expandtabs(100))
89 
90 # x = "hello world"    #在指定范围内,返回l的次数
91 # print(x.count('l',3,10))
92 
93 # x = "hello world"    #中间插入字符串,两边填充*
94 # print(x.center(30,'*'))
字符串总结及练习

二、作业及相关

  1 #! /usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 # __author__ = "DaChao"
  4 # Date: 2017/6/7
  5 
  6 '''
  7 work8:
  8 1.两层while循环,外层的while循环,让用户输入用户名、密码、工作了几个月、每月的工资(整数),
  9 用户名或密码为空,或者工作的月数不为整数,或者月工资不为整数,则重新输入
 10 2.认证成功,进入下一层while循环,打印命令提示,有查询总工资,查询用户身份
 11 (如果用户名为alex则打印super user,如果用户名为yuanhao或者wupeiqi
 12 则打印normal user,其余情况均打印unkown user),退出功能
 13 3.要求用户输入退出,则退出所有循环(使用tag的方式)
 14 
 15 
 16 运行效果如下:
 17 user: egon
 18 password: 123
 19 work_mons: 12
 20 salary: 10000
 21 
 22             1 查询总工资
 23             2 查询用户身份
 24             3 退出登录
 25             
 26 >>: 1
 27 总工资是: 120000.0
 28 
 29             1 查询总工资
 30             2 查询用户身份
 31             3 退出登录
 32             
 33 >>: 2
 34 unkown user
 35 
 36             1 查询总工资
 37             2 查询用户身份
 38             3 退出登录
 39             
 40 >>: 3
 41 '''
 42 
 43 #work8 2
 44 tag = True
 45 
 46 while tag:
 47     while tag:
 48         user = input("Please input your username: ")#解决问题:用户名必须非空格和回车!!!
 49         if len(user) != 0 and not user.isspace():
 50             break
 51     while tag:
 52         password = input("Please input your password: ")
 53         if len(password) != 0 and not password.isspace():
 54             break
 55     workhours = input("Please input your work hours: ")
 56     salary_m = input("Please input your monthly salary: ")
 57     tag = workhours.isdigit() and salary_m.isdigit()
 58     while tag:
 59         print("         1、查询总工资")
 60         print("         2、查询用户身份")
 61         print("         3、退出登录")
 62         order = input("Please input your choose number: ")
 63 #        while order == "1" or "2" or "3":
 64         if order == "1":    #如果不按照惯性输入,怎么办??
 65             print("总工资是: ",int(workhours)*int(salary_m))    #对输入数字取整数!!!
 66         elif order == "2":
 67             if user == "alex":
 68                 print("*******Super user*******")
 69             elif user == "yuanhao" or "wupeiqi":
 70                 print("*******Normal user*******")
 71             else:
 72                 print("*******Unknown user*******")
 73         elif order == "3":
 74             print("*******用户已退出!*******")
 75             tag = False
 76 
 77 
 78 #work8 1
 79 # tag = True
 80 #
 81 # while tag:
 82 #     user = input("Please input your username: ")    #有个问题:直接回车也跳过指令!!!
 83 #     password = input("Please input your password: ")
 84 #     jobtime = input("Please input your time of job: ")
 85 #     salary_m = input("Please input your monthly salary: ")
 86 #     tag = user.isspace() or password.isspace() or not jobtime.isdigit() or not salary_m.isdigit()
 87 #     while not tag:
 88 #         print("         1、查询总工资")
 89 #         print("         2、查询用户身份")
 90 #         print("         3、退出登录")
 91 #         order = input("Please input your choose number: ")
 92 #         if order == "1":
 93 #             print("总工资是: ",int(jobtime)*int(salary_m))
 94 #         elif order == "2":
 95 #             if user == "alex":
 96 #                 print("Super user")
 97 #             elif user == "yuanhao" or "wupeiqi":
 98 #                 print("Normal user")
 99 #             else:
100 #                 print("Unknown user")
101 #         elif order == "3":
102 #             print("用户已退出!")
103 #             break
104 
105 '''
106 work7: 编写while循环,让用户输入内容,判断输入的内容以alex开头的,则将该字符串加上_SB结尾
107 '''
108 
109 # tag = True
110 # while tag:
111 #     content = input("Please input your content: ")
112 #     tag = not content.startswith("alex")
113 # print(content,"SB")
114 
115 '''
116 work6: 编写while循环,让用户输入用户名和密码,如果用户为空或者数字,则重新输入
117 '''
118 
119 # tag = True
120 # while tag:
121 #     user = input("Please input your username: ")
122 #     password = input("Please input your password: ")
123 #     tag = user.isspace() or user.isdigit()
124 # print("You passed!")
125 
126 '''
127 work5: 编写while循环,要求用户输入命令,如果命令为空,则继续输入
128 '''
129 
130 # tag = True
131 # while tag:
132 #     order = input("Please input your order: ")
133 #     tag = order.isspace()
134 # print("You passed.")
135 
136 '''
137 work4: msg='/etc/a.txt|365|get' 将该字符的文件名,文件大小,操作方法切割出来
138 '''
139 
140 # msg = '/etc/a.txt|365|get'
141 # print(msg.split('|'))
142 
143 '''
144 work3: msg='hello alex'中的alex替换成SB
145 '''
146 
147 # msg = 'hello alex'
148 # print(msg.replace("alex","SB"))
149 
150 '''
151 work2: 编写while循环,利用索引遍历出每一个字符
152 '''
153 
154 # w = "My daughter has some cartoon characters on her shirt."
155 # i = 0
156 # while i < len(w):
157 #     print (w[i])
158 #     i+=1
159 
160 
161 '''
162 work1:编写for循环,利用索引遍历出每一个字符
163 '''
164 
165 # w = "My daughter has some cartoon characters on her shirt."
166 #
167 # for i in range(0,len(w)):
168 #     print(w[i])
作业及相关

 

posted @ 2017-06-07 23:19  LiChaoAI  阅读(1257)  评论(0编辑  收藏  举报