Python 基础【二】 下
str()的方法
字符串练习
1.str.capitalize
str.capitalize #返回首字母大写,其他字母小写的字符串
>>> a = 'gwdsr'
>>> a.capitalize()
'gwdsr'
>>>
2.str.casefold
str.casefold #字符串转换成小写,用于不区分大小写的字符串比较
>>> a = 'gwdsr'
>>> b = a.capitalize()
>>> b
'gwdsr'
>>> b.casefold()
'gwdsr'
3.str.center
str.center #返回指定长度的字符串,字符串内容居中,并使用指定字符填充
>>> b.center(40,'-')
'-----------------gwdsr------------------'
4.str.count
str.count #返回子字符串在字符串中出现的次数
>>> b.count('a')
0
>>> b.count('e')
2
>>> b
'gwdsr'
5.str.encode
str.encode #对字符串进行编码,返回字节对象
>>> b.encode('gbk')
b'gwdsr'
6.str.endswith
str.endswith #判断字符串是否以指定的后缀结尾
>>> b
'gwdsr'
>>> b.endswith('r')
True
>>> b.endswith('d')
False
7.str.expandtabs
str.expandtabs #使用空格替换tab
>>> str.expandtabs('hello\tworld')
'hello world'
8.str.find
str.find #返回子字符串在字符串中第一次出现的位置;如没找到,返回-1
>>> c.find('wo')
8
>>> c
'hello world'
9.str.format
str.format #执行字符串格式化操作,替换字段使用{}分隔,替换字段可以是表
>>> str.format('hello {}','world')
'hello world'
10.str.format_map
str.format_map #执行字符串格式化操作,替换字段使用{}分隔,同str.for
11.str.index
str.index #同find(),但如果在字符串中没找到子字符串,会抛出错误
>>> a = 'gwdsr'
>>> a.index('e')
1
>>> a.index('a')
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: substring not found
12.str.isalnum
12.str.isalnum #判断字符串中是否至少有一个字符,并且所有字符都是字母或数字
In [202]: a = 'asf23234d'
In [203]: a.isalnum()
Out[203]: True
In [204]: b = 'asf'
In [205]: b.isalnum()
Out[205]: True
In [206]: c = 'a*sf32'
In [207]: c.isalnum()
Out[207]: False
In [208]: d = ''
In [209]: d.isalnum()
Out[209]: False
13.str.isalpha
13.str.isalpha #判断字符串中是否至少有一个字符,并且所有字符都是字母
In [194]: a
Out[194]: '123'
In [195]: a.isa
a.isalnum a.isalpha
In [195]: a.isalpha()
Out[195]: False
In [196]: b
Out[196]: '1a'
In [197]: b.isa
b.isalnum b.isalpha
In [197]: b.isalpha()
Out[197]: False
In [198]: c
Out[198]: ''
In [199]: c.isalpha()
Out[199]: False
In [200]: d = 'adsf'
In [201]: d.isalpha()
Out[201]: True
14.str.isdecimal
14.str.isdecimal #判断字符串中是否至少有一个字符,并且所有字符都是十进制数字
In [183]: a = '123'
In [184]: b = '1a'
In [185]: c = ''
In [189]: e = '1.3'
In [190]: a.isdecimal()
Out[190]: True
In [191]: b.isdecimal()
Out[191]: False
In [192]: c.isdecimal()
Out[192]: False
In [193]: e.isdecimal()
Out[193]: False
15.str.isdigit
15.str.isdigit #判断字符串中是否至少有一个字符,并且所有字符都是数字
In [183]: a = '123'
In [184]: b = '1a'
In [185]: c = ''
In [186]: a.isdigit()
Out[186]: True
In [187]: b.isdigit()
Out[187]: False
In [188]: c.isdigit()
Out[188]: False
16.str.isidentifier
16.str.isidentifier #判断字符串中是否是有效标识符
In [172]: a
Out[172]: 'gwdsr'
In [173]: b
Out[173]: 'ASDF'
In [174]: a
Out[174]: 'gwdsr'
In [175]: b = '12'
In [176]: c = ''
In [178]: d = '*'
In [179]: a.isidentifier()
Out[179]: True
In [180]: b.isidentifier()
Out[180]: False
In [181]: c.isidentifier()
Out[181]: False
In [182]: b.isidentifier()
Out[182]: False
17.str.islower
17.str.islower #判断字符串中是否小字并且至少有一个字符
In [166]: a = 'gwdsr'
In [167]: b = 'ASDF'
In [168]: c = ''
In [169]: a.islower()
Out[169]: True
In [170]: b.islower()
Out[170]: False
In [171]: c.islower()
Out[171]: False
18.str.isnumeric
18.str.isnumeric #判断字符串中是否至少有一个字符,并且所有字符都是数字字符
In [159]: a = '124'
In [160]: b = 'as234'
In [161]: c = ''
In [162]: a.isnumeric()
Out[162]: True
In [163]: b.isnumeric()
Out[163]: False
In [164]: c.isnumeric()
Out[164]: False
19.str.isprintable
19.str.isprintable #判断字符串的所有字符都是可打印字符或字符串为空
In [151]: a = "\tPuppy"
In [152]: b = "PUPPY\a"
In [153]: c = "puppy"
In [154]: d = "a puppy\b"
In [155]: a.isprintable()
Out[155]: False
In [156]: b.isprintable()
Out[156]: False
In [157]: c.isprintable()
Out[157]: True
In [158]: d.isprintable()
Out[158]: False
20.str.isspace
20.str.isspace #判断字符串中是否至少有一个字符,并且所有字符都是空白字符
In [144]: t2
Out[144]: ''
In [145]: t2.isspace()
Out[145]: False
In [146]: t3 = ' '
In [147]: t3.isspace()
Out[147]: True
In [149]: t
Out[149]: 'gwdsr is a student'
In [150]: t.isspace()
Out[150]: False
21.str.istitle
21.str.istitle #判断字符串中是否至少有一个字符,并且所有字符都是titlecase字符
In [135]: t = 'gwdsr is a student'
In [137]: t1 = t.title()
In [138]: t.istitle()
Out[138]: False
In [139]: t1.istitle()
Out[139]: True
In [142]: t2 = ''
In [143]: t2.istitle()
Out[143]: False
22.str.isupper
22.str.isupper #判断字符串中是否全部是大写字母,空字符串为false
In [124]: a = 'gwdsr'
In [125]: a.isupper()
Out[125]: False
In [126]: a = 'gwdsR'
In [127]: a.isupper()
Out[127]: True
In [128]: b = ''
In [129]: b.isupper()
Out[129]: False
23.str.join
23.str.join #使用字符串作为分隔符串连多个数据为一个字符串
In [116]: a
Out[116]: 'gwdsr'
In [117]: e = ['a','b','c','d']
In [118]: ''.join(e)
Out[118]: 'abcd'
In [119]: '*'.join(e)
Out[119]: 'a*b*c*d'
In [120]: '*'.join(a)
Out[120]: 'p*e*t*e*r'
In [121]: ''.join(a)
Out[121]: 'gwdsr'
### 24.str.ljust
24.str.ljust #返回指定长度的字符串,字符串内容居左,并使用指定字符填充
In [88]: a.ljust(10,'-')
Out[88]: 'gwdsr-----'
In [89]: a.ljust(1,'-')
Out[89]: 'gwdsr'
In [90]: a.ljust(6,'0')
Out[90]: 'gwdsr0'
### 25.str.lower
25.str.lower #字符串转换成小写
In [81]: a = 'gwdsr'
In [82]: a.upper()
Out[82]: 'gwdsR'
In [83]: b = a.upper()
In [84]: b
Out[84]: 'gwdsR'
In [85]: b.lower()
Out[85]: 'gwdsr'
26.str.lstrip
26.str.lstrip #去掉字符串前面的空格,或参数中的字符
27. str.translate / str.maketrans
str.translate #根据table表的映射关系,将字符串中的每个字符转换成另一个#返回一个转换表
In [32]: m = str.maketrans('e','b')
In [33]: a.translate(m)
Out[33]: 'pbtbr'
28.str.partition
28.str.partition #返回包含字符串中分隔符之前、分隔符、分隔符之后的子字符串的tuple
#指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串
In [77]: g = 'www.google.com'
In [79]: g.partition('.')
Out[79]: ('www', '.', 'google.com')
In [80]: g.partition('g')
Out[80]: ('www.', 'g', 'oogle.com')
29.str.replace
29.str.replace #替换字符串中所有的子字符串old为新的字符串new
>>> a.replace('e','A')
'pAtAr'
>>>
30.str.rfind #返回子字符串在字符串中最后一次出现的位置;如没找到,返回-1
31.str.rindex #同rfind(),但如果在字符串中没找到子字符串,会抛出错误
32.str.rjust #返回指定长度的字符串,字符串内容居右,并使用指定字符填充
33.str.rpartition #从后往前查找,返回包含字符串中分隔符之前、分隔符、分隔符之后
34.str.rsplit #从后往前拆分字符串,返回一个列表
35.str.rstrip #去掉字符串后面的空格,或参数中的字符
36.str.split
36.str.split #拆分字符串,返回一个列表
In [58]: a = ' gwdsr is a student '
In [59]: a.split()
Out[59]: ['gwdsr', 'is', 'a', 'student']
37.str.splitlines
37.str.splitlines #字符串以换行符为分隔符拆分,去掉换行符;如果keepends
In [54]: a
Out[54]: 'gwdsr\n is \na student'
In [55]: print(a)
gwdsr
is
a student
In [56]: print(a.splitlines())
['gwdsr', ' is ', 'a student']
)
38.str.startswith
38.str.startswith #判断字符串是否以指定的前缀开始
In [47]: a
Out[47]: ' gwdsr is a student '
In [48]: a.startswith(' ')
Out[48]: True
In [49]: a.startswith('a')
Out[49]: False
39.str.strip
39.str.strip #去掉字符串前后的空格,或指定的所有字符
In [41]: a = ' gwdsr is a student '
In [42]: a
Out[42]: ' gwdsr is a student '
In [43]: a.strip()
Out[43]: 'gwdsr is a student'
40.str.swapcase
40.str.swapcase #大写字符转换成小写字符,小写字符转换成大写字符
In [38]: b = a.title()
In [39]: b
Out[39]: 'gwdsr Is A Student'
In [40]: b.swapcase()
Out[40]: 'gwdsR iS a sTUDENT'
41.str.title
41.str.title #每个单词的第一个字符转换成titlecase字符,其他字符转
In [34]: a = 'gwdsr is a student'
In [35]: a.title()
Out[35]: 'gwdsr Is A Student'
43.str.upper
43.str.upper #字符串转换成大写
In [26]: a.upper()
Out[26]: 'gwdsR'
44.str.zfill
44.str.zfill #在字符串的左边填充0,不会截断字符串
In [24]: a.zfill(10)
Out[24]: '00000gwdsr'
In [25]: a.zfill(5)
Out[25]: 'gwdsr'
builtins Python内置函数
1.abs #求绝对值
2.all #判断迭代器中的所有数据是否都为true
3.any #判断迭代器中的是否有一个数据为true
4.bin #转换整数为一个二进制字符串
5.bool #转换一个数据为布尔值
6.bytearray #将数据转换为字节数组
7.bytes #将数据转换为字节数组
8.callable #判断一个对象是否可调用
9.chr #将整数转成字符
10.classmethod #得到function的classmethod
11.compile #编译source为code或AST对象
12.complex #创建一个复数
13.delattr #删除指定的属性
14.dict #创建一个字典dictionary
15.dir #返回对象的属性列表
16.divmod #得到两个数字相除的结果和余数
17.enumerate #得到一个枚举对象
18.eval #执行一个表达式
19.exec #动态执行Python代码
20.filter #过滤数据得到一个迭代器
21.float #将字符串或数字转为浮点数
22.format #格式化数据
23.frozenset #得到新的frozenset对象
24.getattr #得到对象属性的值
25.globals #得到当前模块的全局符号表的字典
26.hasattr #判断对象是否存在属性
27.hash #得到对象的哈希值
28.help #显示帮助信息
29.hex #整数转换为十六进制表示
30.id #得到对象的id
31.input #输出提示符,读取用户输入
32.int #将数字或字符串转为整数
33.isinstance #判断object是否是classinfo的实例
34.issubclass #判断一个类是否是另一个类的父类
35.iter #得到一个迭代器
36.len #返回对象的长度或集合的数据个数
37.list #创建一个列表
38.locals #得到当前符号表字典
39.map #更改迭代器中的每个数据得到一个新的迭代器
40.max #得到迭代器中最大的或两个或多个参数中最大的
41.min #得到迭代器中最小的或两个或多个参数中最小的
42.next #得到迭代器的下一个数据
43.object #得到object的实例
44.oct #整数转换为八进制表示
45.open #打开文件并返回一个流
46.ord #得到字符的整数表示
47.pow #乘方运算
48.print #输出数据到流
49.property #得到属性
50.range #创建一个范围对象
51.repr #得到对象的字符串表示
52.reversed #反转序列得到一个迭代器
53.round #浮点数按小数位数做舍入操作
54.set #创建一个集合对象
55.setattr #更改属性的值
56.slice #得到分片对象
57.sorted #排序可迭代的数据得到一个列表
58.staticmethod #得到function的staticmethod
59.str #得到对象的str版本
60.sum #计算可迭代数据的合计
61.tuple #创建一个元组
62.type #返回对象的类型或创建一个新的类型对象
63.vars #得到属性信息
dict() 方法练习
1.dict.clear
dict.clear #删除dictionary中的所有key-value对
>>> a = {'k1':'v1'}
>>> a
{'k1': 'v1'}
>>> a.clear()
2.dict.copy
dict.copy #浅拷贝dictionary
>>> b = a.copy()
>>> b
{'k1': 'v1'}
3.dict.fromkeys
dict.fromkeys #返回一个新的dictionary,key由iterable的元素组成,value等于value
>>> a = dict.fromkeys(['k1','k2','k3'],'vvv')
>>> a
{'k3': 'vvv', 'k2': 'vvv', 'k1': 'vvv'}
4.dict.get
dict.get #返回dictionary中key为指定值k对应的value,
>>> a.get('k3')
'vvv'
>>> b = {'k1':a}
>>> b
{'k1': {'k3': 'vvv', 'k2': 'vvv', 'k1': 'vvv'}}
>>> b.get('k1')['k3']
'vvv'
>>>
5.dict.items
dict.items #返回dictionary所有key-value对组成的集合
>>> b.items()
[('k1', {'k3': 'vvv', 'k2': 'vvv', 'k1': 'vvv'})]
6.dict.keys
dict.keys #返回dictionary所有key组成的集合
>>> b.keys()
['k1']
7.dict.pop
dict.pop #从dictionary中删除指定key,返回指定key对应的value。如果dictionary中不存在指定key,如果指定了d,返回d,否则抛出例外
>>> c = b.pop('k1')
>>> c
{'k3': 'vvv', 'k2': 'vvv', 'k1': 'vvv'}
>>> b
{}
>>>
8.dict.popitem
dict.popitem #删除并返回key-value对(key, value)作为2-tuple,如果dictionary为空,抛出例外
>>> e = c.popitem()
>>> e
('k3', 'vvv')
>>> c
{'k2': 'vvv', 'k1': 'vvv'}
>>>
9.dict.setdefau
dict.setdefault #如果dictionary中不存在k,设置D[k]=d
>>> c
{'k2': 'vvv', 'k1': 'vvv'}
>>> c.setdefault('k1','v1')
'vvv'
>>> c
{'k2': 'vvv', 'k1': 'vvv'}
>>> c.setdefault('k4','v4')
'v4'
>>> c
{'k2': 'vvv', 'k1': 'vvv', 'k4': 'v4'}
>>>
10.dict.update
dict.update #使用E(dict/iterable)和F的数据更新dicti
>>> a = {'ak1':'av1'}
>>> b = {'bk1':'bv1'}
>>> b.update(a)
>>> b
{'ak1': 'av1', 'bk1': 'bv1'}
11.dict.values
dict.values #返回dictionary所有value组成的集合
>>> b.values()
['av1', 'bv1']
>>>
list()
列表练习
1.list.append
list.append #附加一个对象到list
>>> a = ['a','b',1,3,4]
>>> a
['a', 'b', 1, 3, 4]
>>> a.append('d')
>>> a
['a', 'b', 1, 3, 4, 'd']
2.list.clear
list.clear #删除list中的所有元素
>>> a.clear()
>>> a
[]
3.list.copy
list.copy #浅拷贝list
>>> a = ['a','b',1,3,4]
>>> a
['a', 'b', 1, 3, 4]
>>> b = a.copy()
>>> b
['a', 'b', 1, 3, 4]
4.list.count
list.count #返回指定参数在list中出现的次数
>>> a.count('b')
1
5.list.extend
list.extend #附加指定iterable中的元素到list
>>> b = [6,6,5,4,2,4]
>>> a.extend(b)
>>> a
['a', 'b', 1, 3, 4, 'd', 6, 6, 5, 4, 2, 4]
>>>
6.list.index
list.index #返回指定值在list中第一次出现的位置,如果list上存在指
>>> a
['a', 'b', 1, 3, 4, 'd', 6, 6, 5, 4, 2, 4]
>>> a.index(1)
2
7.list.insert
list.insert #在list的指定位置index插入object
>>> a.insert(2,'sddsds')
>>> a
['a', 'b', 'sddsds', 1, 3, 4, 'd', 6, 6, 5, 4, 2, 4]
8.list.pop
list.pop #删除并返回指定位置index的元素,默认为最后位置,如果index超过范围或list为空,抛出错误
>>> a
['a', 'b', 'sddsds', 1, 3, 4, 'd', 6, 6, 5, 4, 2, 4]
>>> b = a.pop()
>>> b
4
>>> a
['a', 'b', 'sddsds', 1, 3, 4, 'd', 6, 6, 5, 4, 2]
>>>
9.list.remove
list.remove #从list中删除第一次出现的指定值,如果指定值不存在,抛出例外
>>> a
['a', 'b', 'sddsds', 1, 3, 4, 'd', 6, 6, 5, 4, 2]
>>> a.remove('a')
>>> a
['b', 'sddsds', 1, 3, 4, 'd', 6, 6, 5, 4, 2]
10.list.reverse
list.reverse #反转list中的元素
>>> a
['b', 'sddsds', 1, 3, 4, 'd', 6, 6, 5, 4, 2]
>>> a.reverse()
>>> a
[2, 4, 5, 6, 6, 'd', 4, 3, 1, 'sddsds', 'b']
11.list.sort
list.sort #对list进行排序
>>> a
[2, 4, 5, 6, 6, 'd', 4, 3, 1, 'sddsds', 'b']
>>> a.sort()
>>> a
[1, 2, 3, 4, 4, 5, 6, 6, 'b', 'd', 'sddsds']
>>>
tuple()
元组练习
1.tuple.count
tuple.count #返回指定参数在tuple中出现的次数
>>> b
(1, 2, 3, 4, 4, 5, 6, 6, 'b', 'd', 'sddsds')
>>> b.count(4)
2
2.tuple.index
tuple.index #返回指定值在tuple中第一次出现的位置,如果tuple中不
>>> b
(1, 2, 3, 4, 4, 5, 6, 6, 'b', 'd', 'sddsds')
>>> b.count(4)
2
>>> b.index(2)
1
>>> b.index(1)
0
>>>
Python 基础【二】 下的更多相关文章
- Python 基础 二
Python 基础 二 今天对昨天学习的Python基础知识进行总结,学而不思则惘,思而不学则殆! 一.先对昨天学习的三大循环的使用情况进行总结: 1.while循环的本质就是让计算机在满足某一条件的 ...
- python基础(二)----数据类型
Python基础第二章 二进制 字符编码 基本数据类型-数字 基本数据类型-字符串 基本数据类型-列表 基本数据类型-元组 可变.不可变数据类型和hash 基本数据类型-字典 基本数据类型-集合 二进 ...
- 进击的Python【第二章】:Python基础(二)
Python基础(二) 本章内容 数据类型 数据运算 列表与元组的基本操作 字典的基本操作 字符编码与转码 模块初探 练习:购物车程序 一.数据类型 Python有五个标准的数据类型: Numbers ...
- Python基础(二) —— 字符串、列表、字典等常用操作
一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. 二.三元运算 result = 值1 if 条件 else 值2 如果条件为真:result = 值1如果条件为 ...
- 吾八哥学Python(四):了解Python基础语法(下)
咱们接着上篇的语法学习,继续了解学习Python基础语法. 数据类型大体上把Python中的数据类型分为如下几类:Number(数字),String(字符串).List(列表).Dictionary( ...
- Python菜鸟之路:Python基础(二)
一.温故而知新 1. 变量命名方式 旧的方式: username = 'xxxx' password = 'oooo' 新的方式: username, password = 'xxxx', 'oooo ...
- Python基础二. 数据结构、控制流、运算符、真值测试
一.概述 数据结构上广义上有两种,单一类型和集合类型 单一类型,表示一种对象 集合类型,表示包含多种对象 Python 中的内建的数据类型有str.list.tuple.dict.set.number ...
- python基础二(基础数据类型)
一. 引子 1. 什么是数据 x=10,10是我们要存储的数据 2. 为何数据要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示 3.数据类型 数字 字符串 列表 元组 字 ...
- 【笔记】Python基础二:数据类型之集合,字符串格式化,函数
一,新类型:集合 集合出现之前 python_l = ['lcg','szw','zjw'] linux_l = ['lcg','szw','sb'] #循环方法求交集 python_and_linu ...
- Python基础(下)
前言 print("\n".join([''.join(['*'*((x-y)%3) if((x*0.05)**2+(y*0.1)**2 -1)**3-(x*0.05)**2*(y ...
随机推荐
- C# 基于密码的身份验证报错问题System.Net.NetworkCredential
今天碰到个很奇怪的问题,在用下面这段代码调试时获取身份验证时居然报错,更奇怪的是本地VS中调试正常而在虚机上调试就报错了 ClientCredentials clientCredentials = n ...
- ORACLE里锁有以下几种模式,v$locked_object,locked_mode
ORACLE里锁有以下几种模式: 0:none 1:null 空 2:Row-S 行共享(RS):共享表锁,sub share 3:Row-X 行独占(RX):用于行的修改,sub exclusiv ...
- 09_EGIT插件的安装,Eclipse中克隆(clone),commit,push,pull操作演示
1 下载EGIT,下载地址:http://www.eclipse.org/egit/download/ 最终的下载地址: http://www.eclipse.org/downloads/dow ...
- jQuery 瀑布流插件: Wookmark
原文链接: jQuery Wookmark 在线示例: jQuery Wookmark Demo Wookmark官网: http://www.wookmark.com/jquery-plugin 翻 ...
- Android 开发中遇到Read-only file system问题解决方案
问题描述: 在往scdcard中复制mp3文件时,复制不成功.查看了一下sdcard里面没有内容,且无法直接在里面创建文件会出现-- read only file system类似的内容提示. ...
- 详解Linux2.6内核中基于platform机制的驱动模型 (经典)
[摘要]本文以Linux 2.6.25 内核为例,分析了基于platform总线的驱动模型.首先介绍了Platform总线的基本概念,接着介绍了platform device和platform dri ...
- 使用Material Design Tint和视图详解
视图 首先来讲Material Design 视图的概念,在新的api中,新添加了z轴的概念,z轴垂直于屏幕,用来表现元素的层叠关系,z值(海拔高度)越高,元素离界面底层(水平面)越远,投影越重,这里 ...
- 基于友善之臂ARM-ContexA9-ADC驱动开发
ADC,就是模数转换器,什么是模数转换器? 模数转换器,在电子技术中即是将模拟信号转换成数字信号,也称为数字量化. 当然还有一种叫DAC,就是数模转换,意思相反,即是将数字信号转换成模拟信号. 在友善 ...
- 云技术:负载均衡SLB
什么是SLB? SLB是Server Load Balance(负载均衡)的简称,XX云计算有限公司提供的负载均衡服务,通过设置虚拟服务IP,将位于同一机房的多台云服务器资源虚拟成一个高性能.高可用的 ...
- OVS+DPDK Datapath 包分类技术
本文主体内容译于[DPDK社区文档],但并没有逐字翻译,在原文的基础上进行了一些调整,增加了对TSS分类器的详细阐述. 1. 概览 本文描述了OVS+DPDK中的包分类器(datapath class ...