新学:
成员运算:
in:判断序列中是否包含,若包含为true,不包含为false
if "zhao" in "zhaodigouyou": print("ok") else: print("no")
not in:判断序列中是否不包含,若不包含为true,包含为false
if "zhaoyou" not in "zhaodigouyo": print("ok") else: print("no")
基本数据类型:
数字,int
Python3 里,数字不管多大,永远是int类型
Python2里,数字超过一个限,变为long长整型类
a1 = 123
v = a1.bit.lengtn() 当前数字的二进制至少用几位来表示
a="123" b=int(a) 强制转化为数字 print(b)
num1 = "b" v = int(num1,base=16) base表示进制,把num1按16进制转化为10进制 print(v)
布尔值 bool
字符串 str
字符串部分方法
num1 = "aLex" v = num1.capitalize() #首字母大写 print(v) v1 = num1.casefold() #所有大写变小写,更牛逼,能将很多未知的对应关系变小写 print(v1) v2 = num1.lower() #所有大写变小写, print(v2) #def center(self, width, fillchar=None) fillchar=None,若不带,则默认为None v3 = num1.center(20,"*") #设置宽度,并将内容居中,20代指总长度,*空白位置填充 print(v3)
num1 = "我是爱你的啊" #def ljust(self, width, fillchar=None): v = num1.ljust(20,"*") #宽度20,不足的在右边添加指定字符* print(v)
num1 = "我是爱你的啊" #def rjust(self, width, fillchar=None): v = num1.rjust(20,"*") #宽度20,不足的在左边添加指定字符* print(v)
num1 = "我是爱你的啊" #def rjust(self, width, fillchar=None): v = num1.zfill(20) #宽度20,不足的在左边添加0 print(v)
num1 = "aLexalex" #def count(self, sub, start=None, end=None): v = num1.count(‘ex‘,5,6) #在字符串中寻找子序列出现的次数,5开始位置,6结束位置 print(v)
num1 = "alex" #def endswith(self, suffix, start=None, end=None): v = num1.endswith(‘x‘) #以什么字符结尾,是为true,不是为false print(v) #def startswith(self, prefix, start=None, end=None): v1 = num1.startswith(‘l‘) #以什么字符开始,是为true,不是为false print(v1)
num = "uername\tzty\tpassword\t1234\n966666\t555" #def expandtabs(self, tabsize=8): v = num.expandtabs(6) #断句6,遇到\t就补齐 print(v)
num1 = "alexalex" #def find(self, sub, start=None, end=None): v = num1.find(‘ex‘,5,7) #从开始往后找,找到第一个为止,获取其位置,-1表示未找到 print(v)
num1 = "i am {name},age{a}" print(num1) #def format(self, *args, **kwargs): v = num1.format(name="alex",a=19) #占位符,格式化,将一个字符串中的占位符替换为指定的值 print(v) num1 = "i am {0},age{1}" print(num1) #def format(self, *args, **kwargs): #与上种方式等同, v = num1.format("alex",19) print(v)
num1 = ‘i am {name},age {a}‘ print(num1) #def format_map(self, mapping): v = num1.format_map({"name":‘alex‘,"a":19}) #格式化传入的值{"name":‘alex‘,"a":19}
print(v)
num1 = "alexalex" #def index(self, sub, start=None, end=None): v = num1.index("1") #找不到,报错,忽略使用,用find print(v)
num1 = "alexalex" #def index(self, sub, start=None, end=None): v = num1.index("1") #若找不到,程序报错,忽略使用,用find print(v)
num1 = "uasf7891" #def isalnum(self): v = num1.isalnum() #判断字符串中是否只出现数字和字母 print(v) #输出ture or false
num1 = "我爱你rap" #def isalpha(self): v = num1.isalpha() #判断字符串中是否只出现字母汉字 print(v) #输出ture or false
num1 = "3334442" #def isdecimal(self) v = num1.isdecimal() #判断字符串中是否只出现十进制 print(v)
num1 = "333i4442" #def isdigit(self): v = num1.isdigit() #判断字符串中是否只出现数字,支持特殊数字 print(v)
num1 = "classprint_123" #def isidentifier(self): v = num1.isidentifier() #判断字符串中是否只出现系统标识符 print(v)
num1 = "Alassprint" #def islower(self): v = num1. islower() #判断字符串中是否只出现小写 print(v)
num1 = "23344" #def isnumeric(self): v = num1. isnumeric() #判断字符串中是否只出现数值,支持中文数字 print(v)
num1 = "23344" #def isprintable(self): v = num1. isprintable() #判断字符串中是否只出现可显示,\r\n(换行)\t(制表符)不可显示 print(v)
num1 = " " #def isspace(self): v = num1. isspace() #判断字符串中是否只出现空白字符 print(v)
num1 = "Head Pad" #def istitle(self):title()转化成首字母大写v = num1.istitle() #判断字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。 print(v)
num1 = "HDDDR" #def isupper(self): v = num1.isupper() #判断字符串中所有的单词是否为大写 print(v)
num1 = "我是爱你的啊" t = ‘ ‘ #def join(self, iterable): v = t.join(num1) #将字符串中的每个元素按照指定分隔符进行拼接 print(v)
num1 = "Alex" #def lower(self): v = num1.islower() #判断是否全是小写 v2 = num1.lower() #转化为小写 print(v,v2)
num1 = "Alex" #def lstrip(self, chars=None): v = num1.isupper() #判断是否全是大写 v2 = num1.upper() #转化为大写 print(v,v2)
num1 = "xelex" #def lstrip(self, chars=None): v = num1.lstrip("x") #默认去除左边的空白,及\r\t\n,或指定字符 v2 = num1.rstrip() #默认去除右边的空白,及\r\t\n v3 = num1.rstrip("9lexa") #移除指定字符,有限最多匹配 print(v3)
v = len(num1) #len获取当前字符串中由几个字符串组成
num1 = "你是风儿我是沙" num2 = "去你妈的风和沙" num = "你是风儿我是沙,缠缠绵绵去你家" #def maketrans(self, *args, **kwargs): v = str.maketrans(num1,num2) #对应关系,num2替换num1 v1 = num.translate(v) #根据对应关系进行替换 print(v) print(v1)
num1 = "testasbhjsfg" #def partition(self, sep): v = num1.partition(‘s‘) #进行分割,从第一个s处分割成三份 #def rpartition(self, sep): v1 = num1.rpartition(‘s‘) #从右边进行分割,从第一个s处分割成三份 #def split(self, sep=None, maxsplit=-1): v2 = num1.split(‘s‘,2) #不包含分割的元素,分割2 #def rsplit(self, sep=None, maxsplit=-1): v3 = num1.rsplit(‘s‘,2) #不包含分割的元素,从右边分割 print(v,v1,v2,v3) # print(v1)
num1 = "ssdasdadasd\nssaadasddqd\nasdasdasd" #def splitlines(self, keepends=None): v = num1.splitlines(True) #根据换行符进行分割,ture,false是否保留分隔符 print(v)
num1 = "backend 1.1.1.1" #def startswith(self, prefix, start=None, end=None): v = num1.startswith(‘a‘) #以什么字符开始 #def endswith(self, suffix, start=None, end=None)
v1 = num1.endswith(‘1‘) #以什么字符结尾 print(v)
num1 = "qweQQeq" #def swapcase(self): v = num1.swapcase() #大小写转换 print(v)
比较重要的有:join、split、find、strip、upper、lower、replace
text = "alex" #索引下标,获取字符串中的某个字符 v = text[3] print(v)
text = "alex" #切片 v = text[0:1] #0<= <1,,-1表示到最后一个位置 print(v)
#python 3:len 获取当前字符串中由几个字符组成 text = "屎壳螂" v = len(text) #若在python2中则9个字符 print(v)
text = "获取放弃那的那个美女" #for 循环 : for 变量名 in 字符串: # print(变量名) for a in text: print(a)# break continue 规则也适用与for循环
字符串一旦创建,不可修改
一旦修改或者拼接,都会造成重新生成字符串
text = "alexalexalexalex" #def replace(self, old, new, count=None): v = text.replace("ex","bbb",2) #替换,替换count个 print(v)
#range 创建连续的数字 v = range(100)
#将文字对应的索引打印出来 test = input(">>>") print(test) #test = qwe test[0] l = len(test) #l = 3 print(l) r = range(0,l) #0,3 for item in r: print(item,test[item]) #0 q,1 w,2 e
列表 list
元祖 tuple
字典 dict
原文地址:https://www.cnblogs.com/zhaotianyou/p/10260297.html
时间: 2024-10-31 22:23:06