一、字符串总结与练习

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

作业及相关

转载于:https://www.cnblogs.com/LiChaoAI/p/6959607.html

Day 14 python 之 字符串练习相关推荐

  1. python合并两个字符串_【Python进阶】2.14 合并拼接字符串

    2.14 合并拼接字符串 问题 你想将几个小的字符串合并为一个大的字符串 解决方案 如果你想要合并的字符串是在一个序列或者 iterable 中,那么最快的方式就是使用 join() 方法.比如: & ...

  2. python扫描字符串文本时下线_python字符串处理

    [转]Python 字符串操作方法大全 1.去空格及特殊符号 s.strip().lstrip().rstrip(',') 2.复制字符串 #strcpy(sStr1,sStr2) sStr1 = ' ...

  3. python中字符串格式化%与.format

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  4. 七十二、Python | Leetcode字符串系列(下篇)

    @Author:Runsen @Date:2020/7/3 人生最重要的不是所站的位置,而是内心所朝的方向.只要我在每篇博文中写得自己体会,修炼身心:在每天的不断重复学习中,耐住寂寞,练就真功,不畏艰 ...

  5. python格式化字符串_阿博的Python之路详解String数据类型

    这是阿博的第16篇文章 之前分享了Python的Number数据类型,今天给大家详细讲一下我学习到Python的String数据类型.在日常开发当中String类型应该是我们最常使用到的数据类型,那么 ...

  6. Python之字符串的134个常用操作

    一.字符串切片操作 test = "Python Programming" print("String: ", test)# First one charact ...

  7. python判断字符串结尾-字符串#69301_27种Python字符串操作方法大全

    1.去空格及特殊符号 代码如下: s.strip().lstrip().rstrip(',') 2.复制字符串 代码如下: #strcpy(sStr1,sStr2) sStr1 = 'strcpy' ...

  8. python语言字符串_python中字符串的常见操作方法

    原博文 2019-09-06 09:49 − 1. 字符串概念,字符串是一个容器,包含若干个字符并按照一定的顺序组织成一个整体.字符串支持索引操作. 2. 创建字符串基本语法 变量名 = " ...

  9. python不属于字符串的是_【python cookbook】python过滤字符串中不属于指定集合的字符...

    1 #!/usr/bin/python 2 #-*- coding: utf-8 -*- 3 4 #过滤字符串中不属于指定集合的字符 5 6 importstring7 8 #生成所有字符的可复用的字 ...

  10. Python将字符串转为字典最佳实践

    在工作中我们经常会遇到数据类型之间的互转的问题,而通常我们请求一些API借口返回的结果就是字符串,但是格式是Json的,在Python中转为字典是最易处理的,所以这里记录一下在Python下把字符串转 ...

最新文章

  1. 无线网络实体图生成工具airgraph-ng
  2. 指令系统寻址方式——指令寻址,数据寻址
  3. 笛卡尔集基本原理,等值连接,不等值连接,外连接,自连接
  4. Windows系统Python直接调用C++ DLL
  5. LaTeX技巧205:使用split输入多行公式技巧
  6. npm设置仓库register
  7. [5-24]绿色精品软件每天更新[uc23整理]
  8. Emscripten 单词_人教版高中英语单词表音频(汇总版)
  9. 为什么会有这么多中间表?
  10. 致远OA漏洞学习——A6版本敏感信息泄漏漏洞
  11. 如何听清楚、说明白--《结构思考力》
  12. 2022做跨境为什么要首选Lazada和shopee,现在入驻会面临哪些挑战和机遇?
  13. 基于人工智能推理的英特尔® 精选解决方案
  14. API接口搜索商品列表的调用展示
  15. 问卷星问卷数据怎么快速导入SPSSAU?
  16. 一些常用的数学在线计算器
  17. 查询oracle数据库表名和中文名
  18. 在中国人群中感染率最高的高危型HPV病毒是HPV16、HPV52和HPV58
  19. 二叉树的叶子结点按从左到右的顺序连成一个单链表
  20. 计算机知识学习,网站推荐.

热门文章

  1. SQL知识点脑图(一张图总结SQL)
  2. Spring mvc框架 controller间跳转 ,重定向 ,传参
  3. c语言详解  蔡勒(Zeller)公式计算某一天是星期几  极其方便
  4. 有关onpropertychange事件
  5. 39万的一节课:让你悟透“近朱者赤,近墨者黑”的道理
  6. Tasty项目经验总结(不断补充中)
  7. 用Hello World校验Docker的安装
  8. 魔兽争霸3地图(WarIII Maps):成神之路
  9. 获取图层字段的唯一值集合(ArcEngine)
  10. .NET简谈特性(代码属性)