标签:font pytho 判断 with app char rip 个数 lex
基本数据类型
- 整数
- 布尔值
- 字符串
- 列表
- 元组
- 字典
- 集合
一、字符串:
capitalize() :字符串首字母变大写
user_name = "sunChangxing" user_name.capitalize() #字符串首字母变大写 print(user_name)
casefold() #将所有大写转换为小写(原字符串不变)
user_name = "sunChaNgxing" v = user_name.casefold() #将所有大写转换为小写(原字符串不变) casefold()更高级些不仅仅局限于英文的大小写转换,支持其他语言。 print(user_name) print(v)
user_name = "sunChaNgxing" v = user_name.lower() #将所有大写转换为小写(原字符串不变) print(user_name) print(v)
center(self, width, fillchar=None) #文本居中
user_name = "sunChaNgxing" v = user_name.center(20) print(user_name) 4. 文本居中 # 参数1: 表示总长度# 参数2:空白处填充的字符(长度为1) print(v) v = user_name.center(20,‘@‘) print(user_name) print(v)
count(self, sub, start=None, end=None): #表示传入字符在字符串中出现次数
user_name = "sunChaNgxing" v = user_name.count(‘n‘) print(v) v = user_name.count(‘n‘,2,10) print(v)
endswith(self, suffix, start=None, end=None): #是否以什么结尾
user_name = "sunChaNgxing" v = user_name.endswith(‘g‘) print(v)
startswith(self, prefix, start=None, end=None):#是否以什么开头
user_name = "sunChaNgxing" v = user_name.startswith(‘s‘,2,10) print(v)
encode : #欠着
user_name = "sunChaNgxing" user_name.encode()
expandtabs(self, tabsize=8): #替换制表符 (指定替换宽度包含前面的字符)
user_name = "su\tnC\tha\nNg\txi\tng" v = user_name.expandtabs(5) print(user_name) print(v)
find(self, sub, start=None, end=None): 找到指定子序列的索引位置:不存在返回-1 (常用这个)&&
index(self, sub, start=None, end=None): 找到指定子序列的索引位置:不存在报错
user_name = "sunChaNgxing" v = user_name.find(‘N‘,4,8) print(v)
user_name = "sunChaNgxing" v = user_name.index(‘N‘,4,8) print(v)
format(self, *args, **kwargs): && format_map(self, mapping): 字符串格式化
user_name = "北凉参{0}百万户,其中多少{1}衣裹{2}骨?" v = user_name.format("差","铁","枯") print(v) user_name = "北凉参{name}百万户,其中多少{age}衣裹{sex}骨?" v = user_name.format(name=‘差‘,age=‘铁‘,sex=‘枯‘) print(v) user_name = "北凉参{name}百万户,其中多少{age}衣裹{sex}骨?" v = user_name.format_map({‘name‘:‘铁‘,‘age‘:‘铁‘,‘sex‘:‘枯‘ }) print(v)
isalnum(self): #是否是数字、汉字 isalpha() :是否是汉字
user_name = ‘123123asd嘿嘿嘿‘ v = user_name.isalpha() 汉字 print(v) v1 = user_name.isalnum() 数字,汉字 print(v1)
isdecimal(self):#只能判断阿拉伯数字123... isdigit(self):#阿拉伯 和 ②这类 isnumeric(self):#‘123‘,‘二‘,‘②‘ 判断是否是数字
num = ‘123②二‘ v1 = num.isdecimal() # ‘123‘ v2 = num.isdigit() # ‘123‘,‘②‘ v3 = num.isnumeric() # ‘123‘,‘二‘,‘②‘ print(v1,v2,v3)
isidentifier(self):判断是否为Python中的标识符
user_name = ‘user_name‘ v = user_name.isidentifier() print(v)
islower(self): 是否全部为小写 & isupper(self): 是否全部为大写
user_name = ‘wangbaDa‘ v = user_name.islower() print(v) v1 = user_name.isupper() print(v1)
isspace(self): 是否全部为空格
name = ‘ ‘ v = name.isspace() print(v)
join(self, iterable):元素拼接(元素字符串)
name = ‘alex‘ v = "_".join(name) # 内部循环每个元素 print(v)
ljust(self, width, fillchar=None): rjust(self, width, fillchar=None): 左右填充
name = ‘alex‘ v = name.rjust(20,‘*‘) print(v)
##### 字符串功能总结(常用):
name = ‘alex‘
name.upper()
name.lower()
name.split()
name.find()
name.strip()
name.startswith()
name.format()
name.replace()
"alex".join(["aa",‘bb‘])
# ########################################## int 整数
1. 当前整数的二进制表示,最少位数
age = 4 # 100 print(age.bit_length())
2. 获取当前数据的字节表示
age = 15 v = age.to_bytes(10,byteorder=‘big‘) v = age.to_bytes(10,byteorder=‘little‘) print(v) 00000000 00001111 -> 15
########################################## bool 布尔值 ##########################################
v = 0 # 1,-1
v = ""
v = []
--> 空内容:False
########################################## list 列表 ##########################################
## int=xx; str=‘xxx‘ list=‘xx‘
user_list = [‘李泉‘,‘刘一‘,‘刘康‘,‘豆豆‘,‘小龙‘] # 可变类型 PS: name = ‘alex‘
执行功能;
1.追加
user_list = [‘李泉‘,‘刘一‘,‘刘康‘,‘豆豆‘,‘小龙‘] # 可变类型 user_list.append(‘刘铭‘) print(user_list)
2. 清空
user_list = [‘李泉‘,‘刘一‘,‘刘康‘,‘豆豆‘,‘小龙‘] # 可变类型 user_list.clear() print(user_list)
3. 拷贝(浅拷贝)
user_list = [‘李泉‘,‘刘一‘,‘刘康‘,‘豆豆‘,‘小龙‘] # 可变类型 v = user_list.copy() print(v) print(user_list)
4. 计数
user_list = [‘李泉‘,‘刘一‘,‘李泉‘,‘刘康‘,‘豆豆‘,‘小龙‘] # 可变类型 v = user_list.count(‘李泉‘) print(v)
5. 扩展原列表
user_list = [‘李泉‘,‘刘一‘,‘李泉‘,‘刘康‘,‘豆豆‘,‘小龙‘] # 可变类型 user_list.extend([‘郭少龙‘,‘郭少霞‘]) print(user_list)
6. 查找元素索引,没有报错
user_list = [‘李泉‘,‘刘一‘,‘李泉‘,‘刘康‘,‘豆豆‘,‘小龙‘] # 可变类型 v = user_list.index(‘李海‘) print(v)
7. 删除并且获取元素 - 索引
user_list = [‘李泉‘,‘刘一‘,‘李泉‘,‘刘康‘,‘豆豆‘,‘小龙‘] # 可变类型 v = user_list.pop(1) print(v) print(user_list)
8. 删除 - 值
user_list = [‘李泉‘,‘刘一‘,‘李泉‘,‘刘康‘,‘豆豆‘,‘小龙‘] # 可变类型 user_list.remove(‘刘一‘) print(user_list)
9. 翻转
user_list = [‘李泉‘,‘刘一‘,‘李泉‘,‘刘康‘,‘豆豆‘,‘小龙‘] # 可变类型 user_list.reverse() print(user_list)
10. 排序: 欠参数
nums = [11,22,3,3,9,88] print(nums)
排序,从小到大
nums.sort() print(nums)
从大到小
nums.sort(reverse=True) print(nums)
######################################## tuple:元组,不可被修改的列表;不可变类型
#1. 获取个数
user_tuple = (‘alex‘,‘eric‘,‘seven‘,‘alex‘) v = user_tuple.count(‘alex‘) print(v)
2.获取值的第一个索引位置
user_tuple = (‘alex‘,‘eric‘,‘seven‘,‘alex‘)
v = user_tuple.index(‘alex‘)
print(v)
###### 额外:
user_tuple = (‘alex‘,‘eric‘,‘seven‘,‘alex‘) for i in user_tuple: print(i) v = user_tuple[0] v = user_tuple[0:2] print(v) user_tuple = (‘alex‘,‘eric‘,‘seven‘,[‘陈涛‘,‘刘浩‘,‘赵芬芬‘],‘alex‘) user_tuple[0] = 123 x user_tuple[3] = [11,22,33] x user_tuple[3][1] = ‘刘一‘ print(user_tuple)
li = [‘陈涛‘,‘刘浩‘,(‘alex‘,‘eric‘,‘seven‘),‘赵芬芬‘] ****** 元组最后,加逗号 ****** li = (‘alex‘,) print(li)
######################################### dict:字典: 可变类型 #########################################
1. 清空、
dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘} dic.clear() print(dic)
2. 浅拷贝
dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘} v = dic.copy() print(v)
3. 根据key获取指定的value;不存在不报错
dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘} v = dic.get(‘k1111‘,1111) print(v) v = dic[‘k1111‘] print(v)
4. 删除并获取对应的value值
dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘} v = dic.pop(‘k1‘) print(dic) print(v)
5. 随机删除键值对,并获取到删除的键值
dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘} v = dic.popitem() print(dic) print(v) k,v = dic.popitem() # (‘k2‘, ‘v2‘) print(dic) print(k,v) v = dic.popitem() # (‘k2‘, ‘v2‘) print(dic) print(v[0],v[1])
6. 增加,如果存在则不做操作
dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘} dic.setdefault(‘k3‘,‘v3‘) print(dic) dic.setdefault(‘k1‘,‘1111111‘) print(dic)
7. 批量增加或修改
dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘} dic.update({‘k3‘:‘v3‘,‘k1‘:‘v24‘}) print(dic) dic = dict.fromkeys([‘k1‘,‘k2‘,‘k3‘],123) print(dic) dic = dict.fromkeys([‘k1‘,‘k2‘,‘k3‘],123) dic[‘k1‘] = ‘asdfjasldkf‘ print(dic) dic = dict.fromkeys([‘k1‘,‘k2‘,‘k3‘],[1,]) { k1: 123123213, # [1,2] k2: 123123213, # [1,] k3: 123123213, # [1,] } dic[‘k1‘].append(222) print(dic)
########## 额外:
- 字典可以嵌套
- 字典key: 必须是不可变类型
dic = {
‘k1‘: ‘v1‘,
‘k2‘: [1,2,3,],
(1,2): ‘lllll‘,
1: ‘fffffffff‘,
111: ‘asdf‘,
}
print(dic)
key:
- 不可变
- True,1
dic = {‘k1‘:‘v1‘}
del dic[‘k1‘]
布尔值:
1 True
0 False
bool(1111)
##################################### set,集合,不可重复的列表;可变类型 #####################################
s1 = {"alex",‘eric‘,‘tony‘,‘李泉‘,‘李泉11‘}
s2 = {"alex",‘eric‘,‘tony‘,‘刘一‘}
1.s1中存在,s2中不存在
v = s1.difference(s2)
print(v)
#### s1中存在,s2中不存在,然后对s1清空,然后在重新复制
s1.difference_update(s2)
print(s1)
2.s2中存在,s1中不存在
v = s2.difference(s1)
print(v)
3.s2中存在,s1中不存在
s1中存在,s2中不存在
v = s1.symmetric_difference(s2)
print(v)
4. 交集
v = s1.intersection(s2)
print(v)
5. 并集
v = s1.union(s2)
print(v)
6. 移除
s1 = {"alex",‘eric‘,‘tony‘,‘李泉‘,‘李泉11‘}
s1.discard(‘alex‘)
print(s1)
s1 = {"alex",‘eric‘,‘tony‘,‘李泉‘,‘李泉11‘}
s1.update({‘alex‘,‘123123‘,‘fff‘})
print(s1)
标签:font pytho 判断 with app char rip 个数 lex
原文地址:http://www.cnblogs.com/scxbk/p/6830169.html