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])

作业及相关

时间: 2024-08-26 00:08:35

Day 14 python 之 字符串练习的相关文章

Python格式化字符串~转

Python格式化字符串 在编写程序的过程中,经常需要进行格式化输出,每次用每次查.干脆就在这里整理一下,以便索引. 格式化操作符(%) "%"是Python风格的字符串格式化操作符,非常类似C语言里的printf()函数的字符串格式化(C语言中也是使用%). 下面整理了一下Python中字符串格式化符合: 格式化符号 说明 %c 转换成字符(ASCII 码值,或者长度为一的字符串) %r 优先用repr()函数进行字符串转换 %s 优先用str()函数进行字符串转换 %d / %i

python 之字符串和编码

字符编码 我们已经讲过了,字符串也是一种数据类型,但是,字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte),所以,一个字节能表示的最大的整数就是255(二进制11111111=十进制255),如果要表示更大的整数,就必须用更多的字节.比如两个字节可以表示的最大整数是65535,4个字节可以表示的最大整数是4294967295. 由于计算机是美国人发明的,因此,最早只有1

Python基础-字符串格式化_百分号方式_format方式

Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This PEP proposes a new system for built-in string formatting operations, intended as a replacement for the existing '%' string formatting operator. 1.百分号

python(七)字符串格式化、生成器与迭代器

字符串格式化 Python的字符串格式化有两种方式:百分号方式.format方式 1.百分号的方式 %[(name)][flags][width].[precision]typecode (name)      可选,用于选择指定的key flags          可选,可供选择的值有: +       右对齐:正数前加正好,负数前加负号: -        左对齐:正数前无符号,负数前加负号: 空格    右对齐:正数前加空格,负数前加负号: 0        右对齐:正数前无符号,负数前

Python 的字符串内建函数

Python 的字符串常用内建函数如下: 序号 方法及描述 1 capitalize()     将字符串的第一个字符转换为大写 #!/usr/bin/python3 str = "this is string example from runoob....wow!!!" print ("str.capitalize() : ", str.capitalize()) 以上实例输出结果如下: str.capitalize() : This is string exam

6 Python 数据类型—字符串

字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可. var1 = 'Hello World!' var2 = "Python Runoob" Python访问字符串中的值 Python不支持单字符类型,单字符也在Python也是作为一个字符串使用. Python访问子字符串,可以使用方括号来截取字符串 1 var1 = 'Hello World!' 2 var2 = "Python Ru

python入门—字符串

字符串 字符串是以单引号'或双引号"括起来的任意文本,比如'abc',"123"等等. 请注意,''或""本身只是一种表示方式,不是字符串的一部分,因此,字符串'abc'只有a,b,c这3个字符.如果'本身也是一个字符,那就可以用""括起来,比如"I'm OK"包含的字符是I,',m,空格,O,K这6个字符. 创建字符串: 1 a = 'Hello World!' 2 b = "Python RAlvin&

Python判断字符串是否为字母或者数字(浮点数)

str为字符串s为字符串 str.isalnum() 所有字符都是数字或者字母 str.isalpha() 所有字符都是字母 str.isdigit() 所有字符都是数字 str.isspace() 所有字符都是空白字符.\t.\n.\r 检查字符串是数字/浮点数方法 float部分 >> float('Nan') nan >> float('Nan') nan >> float('nan') nan >> float('INF') inf >>

python中字符串的操作方法

python中字符串的操作方法大全 更新时间:2018年06月03日 10:08:51 作者:骏马金龙 我要评论这篇文章主要给大家介绍了关于python中字符串操作方法的相关资料,文中通过示例代码详细介绍了关于python中字符串的大小写转换.isXXX判断.填充.子串搜索.替换.分割.join以及修剪:strip.lstrip和rstrip的相关内容,需要的朋友可以参考下 前言 python中字符串对象提供了很多方法来操作字符串,功能相当丰富.?123 print(dir(str)) [...