标签:val 分隔符 增删改 整数 set 输入验证码 返回 air 格式化输出
-索引取值
s = ‘alexwusirritian‘
#获取l
n1 = s[1]
#获取最后一个
n2 = s[-1]
-切片取值 s[起始索引:结束索引+1:步长]
#顾头不顾腚
s = ‘alexwusirritian‘
n3 = s[0:4]
#alex的索引为0:3,切片取值需要加一位
n3 = s[:4]
#0可以默认不写!
n3 = s[4:9]
#取值wusir,索引需要加一位
#取值s的全部内容
n4 = s[:]
#步长
n5 = s[4:9:2]
#取值wusir,步长为2,则内容为wsr
-字符串的常用方法:
*** strip 默认去除字符串前后的空格,换行符,制表符。
例:
s = ‘ alexWUsir\t‘
n1 = s.strip()
print(n1)
alexWUsir
-lstrip() #从左边开始去除
-rstrip() #从右边开始去除
username = input(‘请输入用户名:‘).strip()
password = input(‘密码:‘).strip()
if username == ‘老男孩‘ and password == ‘123‘:
print(‘登录成功‘)
请输入用户名: 老男孩
密码:123
登录成功
*** replace 替换
格式:
xx = xx.replace(‘替换字符‘:‘替换后字符‘)
例:
s = ‘李四蹲坑看美女 张三头顶偷窥着‘
h = s.replace(‘李四‘,‘wusir‘)
h2 = s.replace(‘张三‘,‘李四‘)
print(h2)
李四蹲坑看美女 李四头顶偷窥着
***split 指定分隔符, str ---> list
格式:
例:
s1 = ‘alex wusir taibai ritian‘
s2 = ‘alex,wusir,taibai,ritian‘
s3 = ‘alexwusirtaibairitian‘
s4 = ‘alex‘,wusir,taibai,ritian‘
n1 = s1.split() #默认以空格分隔
n2 = s2.split(‘……‘) #指定分隔符
n3 = s3.split(‘a‘) #指定a为分隔符
n4 = s4.split(‘a‘,2) #指定步长为2
***join list ---> str #把列表转换成字符串
格式:
例:
li = [‘alex‘,‘wusir‘,‘ritian‘]
s1 = ‘‘.join(1i)
print(s1)
alexwusirritian <class ‘list‘>
*** find index 通过元素找索引 (字母大小写区分)
find 找不到返回-1
index 找不到会报错
例:s = ‘alexwusir‘
n1 = s.find(‘e‘)
print(n1)
2
n2 = s.find(‘t‘)
print(n1)
-1
*** upper() 字母全部大写
例:
code = ‘EwqAsd‘
username = input(‘请输入用户名:‘)
you_code = input(‘请输入验证码 ‘)
if username == ‘alex‘ and you_code.upper() == code.upper():
print(‘验证成功‘)
请输入用户名:alex
请输入验证码 ewqasd
验证成功
*** lower() 字母全部小写
例:
code = ‘EwqASD‘
username = input(‘请输入用户名:‘)
you_code = input(‘请输入验证码:‘)
if username == ‘lisi‘ and you_code.lower() == code.lower():
print(‘验证成功‘)
请输入用户名:lisi
请输入验证码:ewqasd
验证成功
*** startswith 以.....开头,判断某一个字符的索引位置
例:
s = ‘alexWUsir‘
s6 = s.startswith(‘a‘) #判断是以a开头的吗? True
s6 = s.startswith(‘alex‘) #判断是以alex开头的吗? True
s6 = s.startswith(‘W‘, 4,) #判断是在索引位为4的位置吗? True
print(s6)
*** endswith 以.....结尾
例:
s = ‘alexWUsir‘
s7 = s.endswith(‘sir‘) #判断是以sir为结尾的吗? True
** capitalize 首字母大写,其余字母小写
例:
s = ‘alexWusir‘
n6 = s.capitalize(,)
print(n1)
Alexwusir
** swapcase 大小写翻转
例:
s = ‘alexWusir‘
n7 = s.swapcase()
print(n7)
ALEXwUSIR
* title 非字母隔开的每个单词的首字母大写
例:
s = ‘alex wusir*taibai2ritian‘
n8 = s.title()
print(s3)
Alex Wusir*Taibai2Ritian
*center 设置总长度,并居中
例:
s = ‘坑蒙拐骗偷‘
n9 = s.center(20,‘*‘)
print(n9)
*******坑蒙拐骗偷********
PS:不用填充符号,默认用空格填充n9 = s.center(20)
format 格式化输出
例:
s = ‘我叫:{},今年:{},爱好:{}‘ #必须用花括号
第一种方法(必须一一对应,一个都不能少,少则报错)
n1 = s.format(‘lisi‘,‘20‘,‘女‘)
print(n1)
我叫:lisi,今年:20,爱好:女
第二种方法 (对应所以,按索引顺序填写)
s = ‘我叫:{0},今年:{1},爱好:{2},我依然叫:{0}‘ #必须用花括号
n1 = s.format(‘lisi‘, ‘20‘, ‘女‘)
print(n1)
我叫:lisi,今年:20,爱好:女,我依然叫:lisi
第三种方法
s = ‘我叫:{name},今年:{age},爱好:{hobby}‘
n1 = s.format(age=‘25‘, hobby=‘女‘,name = ‘李四‘)
print(n1)
我叫:李四,今年:25,爱好:女
格式化输出
格式化输出 % 占位符 s d
# name = input(‘请输入名字:‘)
# age = input(‘请输入年龄:‘)
# job = input(‘请输入职业:‘)
# hobbie = input(‘请输入爱好:‘)
# msg = ‘‘‘------------ info of %s -----------
# Name : %s
# Age : %d
# job : %s
# Hobbie: %s
# ------------- end -----------------
# ‘‘‘ % (name, name, int(age), job, hobbie)
# print(msg)
# print(‘我叫%s,今年%d,学习进度2%%‘ % (‘老男孩‘, 10000))
#and or not
# 1,前后都是比较运算
# 优先级:() > not > and > or
print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 > 8)
# 2, 前后都是数值
‘‘‘
0 是False 非0 都是True
x or y if x is True,return x
‘‘‘
print(3 or 4)
print(1 or 4)
print(0 or 4)
print(-1 or 4)
print(3 and 5)
print(3 or 4 and 5)
*************************************************************
列表的增 删 改 查
增:
append() 追加
insert() 按索引插入
extend() 按元素追加
删:
pop 按索引删除。可返回状态
remove 按照元素删除
clear 清空列表
del -按照索引删除
-按照切片删除
-删除列表
改:
按照索引改
list[0] = ‘胖虎‘
按照切片改
list[:3] = ‘胖虎‘
查,自己去看第一天的笔记
************************************************************
字典的增删改查
dic = {"name": "jin", "age": 18, "sex": "male"}
dic2 = {"name": "alex", "weight": 75}
增:dic [‘hobby‘] = ‘女‘ #无则增加,有则修改
dic.setdefault(‘high‘, 169) #有key则不修改,无则添加
删
pop 删除并返回值
print(dic.pop(‘name‘))
{‘age‘: 10000, ‘sex‘: ‘男‘}
clear #清除字典
xx.clear
del
del dic #删除字典
del.dic[‘name‘] #删除键值对
改
dic[‘name‘] = ‘alex‘ # 无责增加,有责修改
update 覆盖,更新字典
查
print(dic[‘name‘]) #直接查
get
print(dic.get(‘name‘))
keys() #列出字典里面的所有‘键’
print(dic.keys())
dict_keys([‘name‘, ‘age‘, ‘sex‘])
values() #列出字典里面所有‘值’
print(dic.values())
dict_values([‘jin‘, 18, ‘male‘])
values():
for ↓ #可以替换
for x in dic.keys(): #循环查看键值对的key,values
print(x)
for x in dic:
print(x)
分别赋值 items
dic = {"name": "jin", "age": 18, "sex": "male"}
dic2 = {"name": "alex", "weight": 75}
for k, v in dic.items():
print(k, v)
name jin
age 18
sex male
更改所有键值对
dic = {"name": "jin", "age": 18, "sex": "male"}
dic2 = {"name": "alex", "weight": 75}
dic = dict.fromkeys(‘abc‘,666)
print(dic)
{‘a‘: 666, ‘b‘: 666, ‘c‘: 666}
dic = {"name": "jin", "age": 18, "sex": "male"}
dic2 = {"name": "alex", "weight": 75}
dic = dict.fromkeys(‘abc‘,[])
dic[‘a‘].append(666)
print(dic)
{‘a‘: 666, ‘b‘: 666, ‘c‘: 666}
例:update
dic = {"name": "jin", "age": 18, "sex": "male"}
dic2 = {"name": "alex", "weight": 75}
dic2.update(dic)
print(dic,dic2)
{‘name‘: ‘jin‘, ‘age‘: 18, ‘sex‘: ‘male‘}
{‘name‘: ‘jin‘, ‘weight‘: 75, ‘age‘: 18, ‘sex‘: ‘male‘}
例: 增加及修改
dic [‘hobby‘] = ‘女‘ #无则增加,有则修改
dic = {
‘name‘: ‘老男孩‘,
‘age‘: 10000,
‘sex‘: ‘男‘,
}
{‘name‘: ‘老男孩‘, ‘age‘: 10000, ‘sex‘: ‘男‘, ‘hobby‘: ‘女‘}
例:
append(追加)
s = [‘WuSir‘, ‘alex‘, ‘OldBoy‘, ‘barry‘]
s.append(‘古力娜扎‘)
print(s)
[‘WuSir‘, ‘alex‘, ‘OldBoy‘, ‘barry‘, ‘古力娜扎‘]
例:
insert(按索引插入)
s = [‘WuSir‘, ‘alex‘, ‘OldBoy‘, ‘barry‘]
s.insert(1,‘古力娜扎‘)
print(s)
[‘WuSir‘, ‘古力娜扎‘, ‘alex‘, ‘OldBoy‘, ‘barry‘]
extend(按元素追加)
s = [‘WuSir‘, ‘alex‘, ‘OldBoy‘, ‘barry‘]
n1 = s.extend(‘abc‘)
n2 = s.extend([‘a‘,123,‘1‘])
print(s)
[‘WuSir‘, ‘alex‘, ‘OldBoy‘, ‘barry‘, ‘a‘, ‘b‘, ‘c‘, ‘a‘, 123, ‘1‘]
删
pop 按照索引删除
remove 按照元素删除
clear 清空列表
del 从内存删除列表
按照索引删除
按照切片删除
例:
pop 按照索引删除 ()pop删除,可返回状态
list = [‘zhangsan‘,‘lisi‘,‘wangwu‘]
n1 = list.pop(1)
print(n1,list)
lisi [‘zhangsan‘, ‘wangwu‘]
remove 按照元素删除
list = [‘zhangsan‘,‘lisi‘,‘wangwu‘]
n2 = list.remove(‘zhangsan‘)
print(n2,list)
None [‘lisi‘, ‘wangwu‘]
clear 清空列表
list = [‘zhangsan‘,‘lisi‘,‘wangwu‘]
n3 = list.clear()
print(list)
[]
del 删除列表
del list
del 按照索引删除
list = [‘zhangsan‘,‘lisi‘,‘wangwu‘]
del list[0]
print(list)
[‘lisi‘, ‘wangwu‘]
del 切片删除
list = [‘zhangsan‘,‘lisi‘,‘wangwu‘,‘zhaoliu‘]
del list[:2]
print(list)
[‘wangwu‘, ‘zhaoliu‘]
改
按照索引改
list = [‘zhangsan‘,‘lisi‘,‘wangwu‘,‘zhaoliu‘]
list[0] = ‘胖虎‘
print(list)
[‘胖虎‘, ‘lisi‘, ‘wangwu‘, ‘zhaoliu‘]
按照切片去改
list = [‘zhangsan‘,‘lisi‘,‘wangwu‘,‘zhaoliu‘]
list[:2] = ‘xvqw‘
print(list)
[‘x‘, ‘v‘, ‘q‘, ‘w‘, ‘wangwu‘, ‘zhaoliu‘]
list = [‘zhangsan‘, ‘lisi‘, ‘wangwu‘, ‘zhaoliu‘]
list[:3] = ‘a‘
print(list)
[‘a‘, ‘zhaoliu‘]
加步长去改
list = [‘zhangsan‘,‘lisi‘,‘wangwu‘,‘zhaoliu‘]
list[:3:2] = ‘ab‘
print(list)
[‘a‘, ‘lisi‘, ‘b‘, ‘zhaoliu‘]
s = ‘AbcdEfgH‘
len 总个数
print(len(s))
8
count 某个元素出现的次数
print(s.count(‘A‘))
1
sort 整数正序排序
num = [1,2,3,4,8,9,6,7,5]
num.sort()
print(num)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
sort(reverse=True) 整数倒叙排序
num = [1, 2, 3, 4, 8, 9, 6, 7, 5]
num.sort(reverse=True)
print(num)
[9, 8, 7, 6, 5, 4, 3, 2, 1]
xcxczx( 翻转)
num = [1, 2, 3, 4, 8, 9, 6, 7, 5]
num.reverse()
print(num)
[5, 7, 6, 9, 8, 4, 3, 2, 1]
字符串的常用方法 字典和列表的:增、删、改、查 format格式化输出。len(长度) count(次数) sort (排序)reverse(翻转)
标签:val 分隔符 增删改 整数 set 输入验证码 返回 air 格式化输出
原文地址:https://www.cnblogs.com/dunkeng/p/9053716.html