一、字符串总结与练习

 #! /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 之 字符串练习的更多相关文章

  1. Python Cookbook(第3版)中文版:15.14 传递Unicode字符串给C函数库

    15.14 传递Unicode字符串给C函数库¶ 问题¶ 你要写一个扩展模块,需要将一个Python字符串传递给C的某个库函数,但是这个函数不知道该怎么处理Unicode. 解决方案¶ 这里我们需要考 ...

  2. Python格式化字符串~转

    Python格式化字符串 在编写程序的过程中,经常需要进行格式化输出,每次用每次查.干脆就在这里整理一下,以便索引. 格式化操作符(%) "%"是Python风格的字符串格式化操作 ...

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

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

  4. Python格式化字符串

    在编写程序的过程中,经常需要进行格式化输出,每次用每次查.干脆就在这里整理一下,以便索引. 格式化操作符(%) "%"是Python风格的字符串格式化操作符,非常类似C语言里的pr ...

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

    字符串格式化 Python的字符串格式化有两种方式:百分号方式.format方式 1.百分号的方式 %[(name)][flags][width].[precision]typecode (name) ...

  6. Python:字符串

    一.序列的概念 序列是容器类型,顾名思义,可以想象,“成员”们站成了有序的队列,我们从0开始进行对每个成员进行标记,0,1,2,3,...,这样,便可以通过下标访问序列的一个或几个成员,就像C语言中的 ...

  7. 教你使用python获得字符串的md5值

    最近需要使用python获取字符串的md5值. 今天把代码贴出来和大家分享一下. 01 #!/usr/bin/env python 02 # -*- coding: cp936 -*- 03 impo ...

  8. python中字符串的操作方法

    python中字符串的操作方法大全 更新时间:2018年06月03日 10:08:51 作者:骏马金龙 我要评论这篇文章主要给大家介绍了关于python中字符串操作方法的相关资料,文中通过示例代码详细 ...

  9. Python基础(字符串和编码)

    字符编码 我们已经讲过了,字符串也是一种数据类型,但是,字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特 ...

随机推荐

  1. 链上链下交互 以太坊Dapp接口开发

    主要是指的是用NodeJs调用 提供接口供前端使用 用户查询和转账 以太坊Dapp项目 众筹项目 功能需求 路人 查看所有众筹项目, 2 @ OK 根据众筹项目的address获取该众筹的详情 (参与 ...

  2. Python攻击

    python   DOS攻击 2版本 #!/usr/bin/env python import socket import time import threading #Pressure Test,d ...

  3. 【iOS开发】initWithNibName、initWithCoder、awakeFromNib和 loadNibNamed详解

    第一.initWithNibName这个方法是在controller的类在IB中创建,但是通过Xcode实例化controller的时候用的. 第二.initWithCoder 是一个类在IB中创建但 ...

  4. PAT 甲级 1006 Sign In and Sign Out

    https://pintia.cn/problem-sets/994805342720868352/problems/994805516654460928 At the beginning of ev ...

  5. 玩adb和fastboot

    http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380143fd3d1027fa3c215cc790a1b18 ...

  6. Netscaler的超高端口复用助力应对公网地址紧张

    Netscaler的超高端口复用助力应对公网地址紧张 http://blog.51cto.com/caojin/1898351 经常会有人问一个IP只有65535(姑且不考虑预留端口),从Big-ip ...

  7. 【转】Win7装不上Office2010 提示MSXML 6.10.1129.0

    转自:http://zhidao.baidu.com/link?url=aZPbpBu0Fb7rc8HCb_NuonuZ4ET_BB8_NgZ96tCpB9dyuUyWVwMl78MLa7rh-rfx ...

  8. [Leetcode] Merge k sorted lists 合并k个已排序的链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...

  9. BZOJ4785 [Zjoi2017]树状数组 【二维线段树 + 标记永久化】

    题目链接 BZOJ4785 题解 肝了一个下午QAQ没写过二维线段树还是很难受 首先题目中的树状数组实际维护的是后缀和,这一点凭分析或经验或手模观察可以得出 在\(\mod 2\)意义下,我们实际求出 ...

  10. spring中Constructor、@Autowired、@PostConstruct的顺序【转】

    其实从依赖注入的字面意思就可以知道,要将对象p注入到对象a,那么首先就必须得生成对象p与对象a,才能执行注入.所以,如果一个类A中有个成员变量p被@Autowired注解,那么@Autowired注入 ...