Python之路【第二篇】:Python基础
一、基本数据类型 数字(int)
如:21、45、56
每一个整数都具备如下功能:
1 - int 2 将字符串转换为数字 3 例子: 4 a = "123" 5 print(type(a),a) 6 输出结果: 7 >>> a = "123" 8 >>> print(type(a),a) 9 <class 'str'> 123 10 11 b = int(a) 12 print(type(b),b) 13 14 输出结果: 15 >>> b = int(a) 16 >>> print(type(b),b) 17 <class 'int'> 123 18 19 num = "0022" 20 v = int(num, base=16) 21 print(v) 22 23 输出结果: 24 >>> num = "0022" 25 >>> v = int(num, base=16) 26 >>> print(v) 27 34 28 29 - bit_length 30 #当前数字的二进制,至少用n位表示 31 age = 22 32 v = age.bit_length() 33 print(v) 34 35 输出结果: 36 >>> age = 22 37 >>> v = age.bit_length() 38 >>> print(v) 39 5字符串(str)
1、name.capitalize() #首字母大写
例子:
>>> test = "zhUrui" >>> v = test.capitalize() >>> print(v) Zhurui
2、name.casefold() #所有变小写,casefold更牛逼,很多未知的对相应变小写
例子:
>>> test = "zhUrui" >>> v1 = test.casefold() >>> print(v1) zhurui >>> v2 = test.lower() >>> print(v2) zhurui
3、name.center() #设置宽度,并将内容居中
name.ljust() #设置宽度,变量向左,其他部分用所定义的填充符 填充
name.rjust() #设置宽度,变量向右,其他部分用所定义的填充符 填充
name.zfill() #设置宽度,默认变量向右,其他部分用zfill方法特定的填充符"000" 填充
>>> test = "zhurui" >>> v = test.center(20,"中") >>> print(v) 中中中中中中中zhurui中中中中中中中 解释: # 20 代指总长度 # * 空白未知填充,一个字符,可有可无 输出结果: 中中中中中中中zhurui中中中中中中中 ########################################## >>> test = "zhurui" >>> v = test.ljust(20,"*") #ljust 变量靠左,其他部分用*填充 >>> print(v) zhurui************** ################################## >>> test = "zhurui" >>> v = test.rjust(20,"*") #rjust 变量靠右,其他部分用*填充 >>> print(v) **************zhurui
>>> test = "zhurui" >>> v = test.zfill(20) ##zfill只能用于000填充 >>> print(v) 00000000000000zhurui
4、name.count() #去字符串中寻找,寻找子序列的出现次数
>>> test = "Zhuruizhuruiru" >>> v = test.count('ru') >>> print(v) 3 >>> v = test.count('z') >>> print(v) 1 ######################################### >>> test = "Zhuruizhuruiru" >>> v = test.count('ru', 5, 6) >>> print(v) 0
5、name.encode() #将字符串编码成bytes格式
6、name.decode()
7、name.endswith("ui") #判断字符串是否以ui结尾
name.startswith('ui') #判断字符串是否以ui开始
>>> test = "zhurui" >>> v = test.endswith('ui') >>> print(v) True >>> v = test.startswith('ui') >>> print(v) False
8、"Zhu\tRui".expandtabs(10) #输出‘Zhu Rui’, 将\t转换为多长的空格
>>> test = "Zhu\tRui" >>> v = test.expandtabs(10) >>> print(v) Zhu Rui ################################ test = "username\tpassword\temail\nzhurui\t123456\t24731701@qq.com\nzhurui\t123456\t24731701@qq.com\nzhurui\t123456\t24731701@qq.com" v = test.expandtabs(20) print(v) 输出结果: C:\Python35\python3.exe C:/Users/ZR/PycharmProjects/python全栈开发/day1/logging.py username password email zhurui 123456 24731701@qq.com zhurui 123456 24731701@qq.com zhurui 123456 24731701@qq.com
9、name.find(A) #从开始往后找,找到第一个之后,获取其位置即索引,找不到返回-1
## > 或 >=
# 未找到 -1
>>> test = "williamwilliam" >>> v = test.find('am') >>> print(v) 5 >>> v = test.find('t') #找变量中的"t"字符, >>> print(v) -1 ##没有找到,返回负一
10、name.index('a') #找不到,报错
>>> test = "williamwilliam" >>> v = test.index('a') >>> print(v) 5 >>> v = test.index('8') ##找字符串中是否 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found
11、name.format() #格式化,将一个字符串中的占位符替换为指定的值
>>> test = 'i am {name}, age {a}' >>> print(test) i am {name}, age {a} >>> v = test.format(name='william',a=22) >>> print(v) i am william, age 22
>>> test = 'i am {0},age {1}' >>> print(test) i am {0},age {1} >>> v = test.format('william', 22) >>> print(v) i am william,age 22
12、name.format_map() #格式化, 传入的值{"name": 'william', "a": 22}
>>> test = 'i am {name}, age {a}' >>> v1 = test.format(name='zhurui',a=22) >>> v2 = test.format_map({"name":'zhurui', "a": 19}) >>> print(v1) i am zhurui, age 22 >>> print(v2) i am zhurui, age 19
13、name.isalnum() #字符串中是否只包含 字母和数字
>>> test = "234" >>> v = test.isalnum() >>> print(v) True
14、name.isalpha() #是否是字母,汉字
>>> test = "asfdge242" >>> v = test.isalpha() >>> print(v) False >>> test = "威廉" >>> v = test.isalpha() >>> print(v) True
15、判断输入的是否是数字
>>> test = "二" # 1 , ② >>> v1 = test.isdecimal ##判断十进制小数 >>> v2 = test.isdigit() >>> v3 = test.isnumeric() ##判断汉语的数字,比如 "二" >>> print(v1,v2,v3) <built-in method isdecimal of str object at 0x00000201FE440AB0> False True
16、name.isprintable() #判断是否存在不可显示的字符
\t 制表符
\n 换行
>>> test = "qepoetewt\tfdfde" >>> v = test.isprintable() >>> print(v) False >>> test = "qepoetewtfdfde" >>> v = test.isprintable() >>> print(v) True
17、name.isspace() #判断是否全部是空格
>>> test = "" >>> v = test.isspace() >>> print(v) False >>> test = " " >>> v = test.isspace() >>> print(v) True
18、name.istitle() #判断是否是标题,其中必须首字母大写
>>> test = "Return True if all cased characters in S are uppercase" >>> v1 = test.istitle() >>> print(v1) False >>> v2 = test.title() #将字符串首字母大写 >>> print(v2) Return True If All Cased Characters In S Are Uppercase >>> v3= v2.istitle() >>> print(v3) True
19、***** name.join() #将字符串中的每一个元素按照指定分隔符进行拼接(五星重点参数)
>>> test = "出任CEO迎娶白富美" >>> print(test) 出任CEO迎娶白富美 >>> v = '_'.join(test) >>> print(v) 出_任_C_E_O_迎_娶_白_富_美 >>>
20、name.islower() #判断是否全部是大小写 和 转换为大小写
>>> test = "William" >>> v1 =test.islower() #判断是否全都是小写 >>> v2 = test.lower() #将变量转换为小写 >>> print(v1, v2) False william >>> ################################### >>> test = "William" >>> v1 =test.isupper() #判断是否全都是大写 >>> v2 = test.upper() #将变量转换为大写 >>> print(v1, v2) False WILLIAM
21、移除指定字符串,优先最多匹配
>>> test = 'xa' >>> v1 =test.isupper() >>> v = test.lstrip("xa") >>> print(v) >>> v = test.rstrip("92exxxexxa") >>> print(v) >>> v = test.strip("xa") >>> print(v) ################################### # test.lstrip() # test.rstrip() # test.strip() # 去除左右空白 # v = test.lstrip() # v = test.rstrip() # v = test.strip() # print(v) # print(test) # 去除\t \n # v = test.lstrip() # v = test.rstrip() # v = test.strip() # print(v)
22、对应关系替换
>>> test = "aeiou" >>> test1 = "12345" >>> v = "asidufkasd;fiuadkf;adfkjalsdjf" >>> m = str.maketrans("aeiou", "12345") >>> new_v = v.translate(m) >>> print(new_v) 1s3d5fk1sd;f351dkf;1dfkj1lsdjf
23、name.partition() #分割为三部分
>>> test = "testegerwerwegwewe" >>> v = test.partition('s') >>> print(v) ('te', 's', 'tegerwerwegwewe') >>> v = test.rpartition('s') >>> print(v) ('te', 's', 'tegerwerwegwewe') >>>
24、name.split() #分格为指定个数
>>> test = "sagesgegessress" >>> v = test.split('s',2) >>> print(v) ['', 'age', 'gegessress'] >>>
25、分割, 只能根据,true, false:是否保留换行
>>> test = "fwerwerdf\frweqnndasfq\fnaqewrwe" >>> v = test.splitlines(False) >>> print(v) ['fwerwerdf', 'rweqnndasfq', 'naqewrwe']
26、以xxx开头,以xx结尾
>>> test = "backend 1.2.3.4" >>> v = test.startswith('a') >>> print(v) False >>> test.endswith('a') False
27、name.swapcase() #大小写转换
>>> test = "WiiLiAm" >>> v = test.swapcase() >>> print(v) wIIlIaM
28、name.isidentifier() #字母,数字,下划线 :标识符 def class
>>> a = "def" >>> v = a.isidentifier() >>> print(v) True
29、name.replace() #将指定字符串替换为指定字符串,替换功能相当于sed
>>> test = "williamwilliamwilliam" >>> v = test.replace("am", "bbb") >>> print(v) willibbbwillibbbwillibbb >>> v = test.replace("am", "bbb",2) >>> print(v) willibbbwillibbbwilliam >>>
下一篇: 一牛X人物来澡堂泡澡爆笑大全