Day 14 python 之 字符串练习
一、字符串总结与练习
#! /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])
作业及相关
Day 14 python 之 字符串练习的更多相关文章
- Python Cookbook(第3版)中文版:15.14 传递Unicode字符串给C函数库
15.14 传递Unicode字符串给C函数库¶ 问题¶ 你要写一个扩展模块,需要将一个Python字符串传递给C的某个库函数,但是这个函数不知道该怎么处理Unicode. 解决方案¶ 这里我们需要考 ...
- Python格式化字符串~转
Python格式化字符串 在编写程序的过程中,经常需要进行格式化输出,每次用每次查.干脆就在这里整理一下,以便索引. 格式化操作符(%) "%"是Python风格的字符串格式化操作 ...
- Python基础-字符串格式化_百分号方式_format方式
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
- Python格式化字符串
在编写程序的过程中,经常需要进行格式化输出,每次用每次查.干脆就在这里整理一下,以便索引. 格式化操作符(%) "%"是Python风格的字符串格式化操作符,非常类似C语言里的pr ...
- python(七)字符串格式化、生成器与迭代器
字符串格式化 Python的字符串格式化有两种方式:百分号方式.format方式 1.百分号的方式 %[(name)][flags][width].[precision]typecode (name) ...
- Python:字符串
一.序列的概念 序列是容器类型,顾名思义,可以想象,“成员”们站成了有序的队列,我们从0开始进行对每个成员进行标记,0,1,2,3,...,这样,便可以通过下标访问序列的一个或几个成员,就像C语言中的 ...
- 教你使用python获得字符串的md5值
最近需要使用python获取字符串的md5值. 今天把代码贴出来和大家分享一下. 01 #!/usr/bin/env python 02 # -*- coding: cp936 -*- 03 impo ...
- python中字符串的操作方法
python中字符串的操作方法大全 更新时间:2018年06月03日 10:08:51 作者:骏马金龙 我要评论这篇文章主要给大家介绍了关于python中字符串操作方法的相关资料,文中通过示例代码详细 ...
- Python基础(字符串和编码)
字符编码 我们已经讲过了,字符串也是一种数据类型,但是,字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特 ...
随机推荐
- MySQL linux错误处理
https://cloud.tencent.com/developer/article/1023732 mysql5.7 ERROR 1045 (28000): Access denied for u ...
- HDU 3698 Let the light guide us(DP+线段树)(2010 Asia Fuzhou Regional Contest)
Description Plain of despair was once an ancient battlefield where those brave spirits had rested in ...
- winform 外部组件发生异常
问题描述.先前程序运行可以正常,但是突然这次调试就弹出此异常: 解决方法:1.本次调试是不是重新生成解决方案了?应该是因为之前的解决方案删了,把引用的dll(包含此dll关联的dll)自动删掉: 2. ...
- C - 安装雷达
C - 安装雷达 Time Limit: 1000/1000MS (C++/Others) Memory Limit: 65536/65536KB (C++/Others) Problem Descr ...
- CodeForces Round #521 (Div.3) D. Cutting Out
http://codeforces.com/contest/1077/problem/D You are given an array ss consisting of nn integers. Yo ...
- Chrome Extension & Dark Theme
Chrome Extension & Dark Theme https://chrome.google.com/webstore/detail/eimadpbcbfnmbkopoojfekhn ...
- [STL] STL各容器实现原理
STL共有六大组件1.容器 2.算法 3.迭代器 4.仿函数 6.适配器 STL容器的实现原理 STL来管理数据十分方便,省去了我们自己构建数据结构的时间.其实,STL的实现也是基于我们常见的数据结构 ...
- 【bzoj1018】[SHOI2008]堵塞的交通traffic 线段树区间合并+STL-set
题目描述 给出一张2*n的网格图,初始每条边都是不连通的.多次改变一条边的连通性或询问两个点是否连通. 输入 第一行只有一个整数C,表示网格的列数.接下来若干行,每行为一条交通信息,以单独的一行“Ex ...
- 【电影影评】梦之安魂曲-败给了BGM和豆瓣影评
首先,这部电影豆瓣8.7分,一般来说,豆瓣的打分是比较准确的.能反映一个片子的质量,而较少受到环境的影响.但是这种关系当然也不全对,比如某些片子可能特别让某一种人喜欢(如退役军人和军旅题材),而在某些 ...
- perl的Sys::Syslog模块(openlog,syslog,closelog函数,setlogsock)-自定义日志
perl的Sys::Syslog模块(openlog,syslog,closelog函数,setlogsock)-自定义日志 http://blog.chinaunix.net/xmlrpc.php? ...