python字符串内置函数

1、字符串

定义:它是一个有序的字符的集合,用于存储和表示基本的文本信息,‘’或“”或‘’‘ ’‘’中间包含的内容称之为字符串
特性:
1.只能存放一个值
2.不可变
3.按照从左到右的顺序定义字符集合,下标从0开始顺序访问,有序
补充:
  1.字符串的单引号和双引号都无法取消特殊字符的含义,如果想让引号内所有字符均取消特殊意义,在引号前面加r,如name=r'l\thf'
  2.unicode字符串与r连用必需在r前面,如name=ur'l\thf'

2、字符串常用操作

1
2
3
4
5
6
# 1字母处理:
.upper()     # 全部大写
.lower()     # 全部小写
.swapcase()     # 大小写互换
.capitalize()     # 首字母大写,其余小写
.title()     # 首字母大写
  View Code
1
2
3
4
5
6
# 2格式化相关
 
.ljust(width)      # 获取固定长度,左对齐,右边不够用空格补齐
.rjust(width)      # 获取固定长度,右对齐,左边不够用空格补齐
.center(width)   # 获取固定长度,中间对齐,两边不够用空格补齐
.zfill(width)       # 获取固定长度,右对齐,左边不足用0补齐
  View Code

 

1
2
3
4
5
6
7
8
# 3 字符串搜索相关
 
.find()     # 搜索指定字符串,没有返回-1
.index()     # 同上,但是找不到会报错
.rfind()     # 从右边开始查找
.count()     # 统计指定的字符串出现的次数
 
# 上面所有方法都可以用index代替,不同的是使用index查找不到会抛异常,而find返回-1
s='hello world'
print(s.find('e'))  # 搜索指定字符串,没有返回-1
print(s.find('w',1,2))  # 顾头不顾尾,找不到则返回-1不会报错,找到了则显示索引
print(s.index('w',1,2)) # 同上,但是找不到会报错
print(s.count('o')) # 统计指定的字符串出现的次数
print(s.rfind('l')) # 从右边开始查找

 

复制代码
# 4字符串替换

.replace('old','new')    # 替换old为new
.replace('old','new',次数)    # 替换指定次数的old为new


s='hello world'
print(s.replace('world','python'))
print(s.replace('l','p',2))
print(s.replace('l','p',5))

执行结果:
hello python
heppo world
heppo worpd
复制代码

 

复制代码
# 5字符串去空格及去指定字符

.strip()    # 去两边空格
.lstrip()    # 去左边空格
.rstrip()    # 去右边空格

.split()    # 默认按空格分隔
.split('指定字符')    # 按指定字符分割字符串为数组


s='   h e-l lo   '
print(s)
print(s.strip())
print(s.lstrip())
print(s.rstrip())
print(s.split('-'))
print(s.split())
复制代码

 

复制代码
# 6字符串判断相关

.startswith('start')    # 是否以start开头
.endswith('end')    # 是否以end结尾
.isalnum()    # 是否全为字母或数字
.isalpha()    # 是否全字母
.isdigit()    # 是否全数字
.islower()    # 是否全小写
.isupper()    # 是否全大写
.istitle()    # 判断首字母是否为大写
.isspace()    # 判断字符是否为空格

# 补充
bin()    # 十进制数转八进制
hex() # 十进制数转十六进制
range() # 函数:可以生成一个整数序列
type() # 查看数据类型
len() # 计算字符串长度
format() # 格式化字符串,类似%s,传递值能多不能少
 
复制代码

 3、python中str函数isdigit、isdecimal、isnumeric的区别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
isdigit()
True Unicode 数字,byte数字(单字节),全角数字(双字节),罗马数字
False : 汉字数字
Error: 无
 
isdecimal()
True Unicode 数字,,全角数字(双字节)
False : 罗马数字,汉字数字
Error: byte数字(单字节)
 
isnumeric()
True Unicode 数字,全角数字(双字节),罗马数字,汉字数字
False : 无
Error: byte数字(单字节)

 4、内置函数

  •     数学运算(7个)
  •     类型转换(24个)
  •     序列操作(8个)
  •     对象操作(7个)
  •     反射操作(8个)
  •     变量操作(2个)
  •     交互操作(2个)
  •     文件操作(1个)
  •     编译执行(4个)
  •     装饰器(3个)

数学运算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
abs :求数值的绝对值
abs ( - 2 )
 
divmod :返回两个数值的商和余数
divmod ( 5 , 2 )
divmod ( 5.5 , 2 )
 
max :返回迭代对象中的元素的最大值或者所有参数的最大值
max ( 1 , 2 , 3 )     # 传入3个参数 取3个中较大者
max ( '1234' )     # 传入1个可迭代对象,取其最大元素值
max ( - 1 , 0 ,key = abs )     # 传入了求绝对值函数,则参数都会进行求绝对值后再取较大者
 
min :返回可迭代对象中的元素的最小值或者所有参数的最小值
min ( 1 , 2 , 3 )   # 传入3个参数 取3个中较小者
min ( '1234' )     # 传入1个可迭代对象,取其最小元素值
min ( - 1 , - 2 ,key = abs )     # 传入了求绝对值函数,则参数都会进行求绝对值后再取较小者
 
pow :返回两个数值的幂运算值或其余指定整数的模值
pow ( 2 , 3 )
 
round :对浮点数进行四舍五入求值
round ( 1.1111 , 1 )
 
sum :对元素类型是数值的可迭代对象中的每个元素求和
sum (( 1 , 2 , 3 , 4 ))     # 传入可迭代对象、元素类型必须是数值型

类型转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
bool :根据传入的参数的逻辑值创建一个新的布尔值
bool ()或 bool ( 0 )      # 数值0、空值为False
 
int :根据传入的参数创建一个新的整数
int ()      # 不传入参数时,得到结果0
 
float :根据传入的参数创建一个新的浮点数
float ()     # 不提供参数的时候,返回0.0
 
complex :根据传入参数创建一个新的复数
complex ()     # 当两个参数都不提供时,返回复数 0j
 
str :返回一个对象的字符串表现形式(给用户)
 
bytearray:根据传入的参数创建一个新的字节数组
bytearray( '中文' , 'utf-8'
bytearray(b '\xe4\xb8\xad\xe6\x96\x87' )
 
bytes:根据传入的参数创建一个新的不可变字节数组
bytes( '中文' , 'utf-8' )
b '\xe4\xb8\xad\xe6\x96\x87'
 
memoryview:根据传入的参数创建一个新的内存查看对象
v = memoryview(b 'asdf' )
print (v[ 0 ])     # 97
print (v[ - 1 ])     # 102
 
ord :返回 Unicode 字符对应的整数
print ( ord ( 'a' ))
 
chr :返回整数所对应的 Unicode 字符
print ( chr ( 97 ))
 
bin :将整数转换成 2 进制字符串
oct :将整数转化成 8 进制数字符串
hex :将整数转换成 16 进制字符串
 
tuple :根据传入的参数创建一个新的元组
list :根据传入的参数创建一个新的列表
dict :根据传入的参数创建一个新的字典
set :根据传入的参数创建一个新的集合
 
frozenset :根据传入的参数创建一个新的不可变集合
a = frozenset ( range ( 10 ))
print (a)
# frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
 
enumerate :根据可迭代对象创建枚举对象
l1 = [ 'one' , 'two' , 'three' , 'five' ]
print ( list ( enumerate (l1)))
# [(0, 'one'), (1, 'two'), (2, 'three'), (3, 'five')]
print ( list ( enumerate (l1,start = 1 )))   # 指定起始值
# [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'five')]
 
range :根据传入的参数创建一个新的 range 对象
iter :根据传入的参数创建一个新的可迭代对象
a = iter ( 'asdf' )
print (a)     # <str_iterator object at 0x00000190B4D99668>
print ( next (a))   # a
print ( next (a))   # s
print ( next (a))   # d
print ( next (a))   # f
print ( next (a))   # 报错StopIteration
 
slice :根据传入的参数创建一个新的切片对象
c1 = slice ( 5 )
print (c1)    # slice(None, 5, None)
c1 = slice ( 2 , 5 )
print (c1)    # slice(2, 5, None)
c1 = slice ( 1 , 4 , 7 )
print (c1)    # slice(1, 4, 7)
 
super :根据传入的参数创建一个新的子类和父类关系的代理对象
# 定义父类A类
class  A( object ):
     def  __init__( self ):
         print (A.__init__)
 
# 定义子类,继承A
class  B(A):
     def  __init__( self ):
         print (B.__init__)
         super ().__init__()
 
# super调用父类方法
b = B()
print (b)
<function B.__init__ at  0x0000023DB0CA76A8 >
<function A.__init__ at  0x0000023DB0CA7620 >
 
object :创建一个新的 object 对象
1
<span style = "font-size: 14pt" ><strong>序列操作< / strong>< / span>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
all :判断可迭代对象的每个元素是否都为 True
print ( all ([ 1 , 2 ]))     # 列表中每个元素逻辑值均为True,返回True
print ( all ([ 0 , 2 ]))      # 列表中0的逻辑值为False,返回False
 
any :判断可迭代对象的元素是否有为 True 值的元素
# 列表元素有一个为True,则返回True
# 列表元素全部为False,则返回False
 
filter :使用指定方法过滤可迭代对象的元素
 
map :使用指定方法去作用传入的每个可迭代对象的元素,生成新的可迭代对象
 
next :返回可迭代对象中的下一个元素值
# 传入default参数后,如果可迭代对象还有元素没有返回,则依次返回其元素值,如果所有元素已经返回,则返回default指定的默认值而不抛出StopIteration 异常
  
reversed :反转序列生成新的可迭代对象
 
sorted :对可迭代对象进行排序,返回一个新的列表
 
zip :聚合传入的每个迭代器中相同位置的元素,返回一个新的元组类型迭代器

对象操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
help :返回对象的帮助信息
dir :返回对象或者当前作用域内的属性列表
id :返回对象的唯一标识符
hash :获取对象的哈希值
type :返回对象的类型,或者根据传入的参数创建一个新的类型
len :返回对象的长度
ascii:返回对象的可打印表字符串表现方式
format :格式化显示值
 
vars :返回当前作用域内的局部变量和其值组成的字典,或者返回对象的属性列表
class  A( object ):
     pass
 
a = A()
print (a.__dict__)    # {}
print ( vars (a))       # {}
a.name = 'buer'
print (a.__dict__)    # {'name': 'buer'}
print ( vars (a))       # {'name': 'buer'}

反射操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
__import__ :动态导入模块
print ( __import__ ( 'os' ))
print ( __import__ ( 'time' ))
 
# <module 'os' from 'D:\\Python36\\lib\\os.py'>
# <module 'time' (built-in)>
 
isinstance :判断对象是否是类或者类型元组中任意类元素的实例
issubclass :判断类是否是另外一个类或者类型元组中任意类元素的子类
 
hasattr :检查对象是否含有属性
class  Student:
     def  __init__( self ,name):
         self .name = name
 
s = Student( 'Ethan' )
print ( hasattr (s, 'name' ))     # 含有name属性为True
print ( hasattr (s, 'age' ))      # 不含有age属性为False
 
getattr :获取对象的属性值
print ( getattr (s, 'name' ))     # 存在属性name,Ethan
print ( getattr (s, 'age' , 20 ))   # 不存在属性age,但提供了默认值,返回默认值
print ( getattr (s, 'age' ))      # 不存在属性age,未提供默认值,调用报错
报错如下:
Traceback (most recent call last):
   File  "D:/test.py" , line  30 in  <module>
     print ( getattr (s, 'age' ))
AttributeError:  'Student'  object  has no attribute  'age'
 
setattr :设置对象的属性值
print (s.name)    # Ethan
setattr (s, 'name' , 'Tom' )    # name属性存在,做赋值操作
setattr (s, 'age' , 18 )      # age属性不存在,创建这个属性
print (s.name)    # Tom
print (s.age)     # 18
 
delattr :删除对象的属性
class  Student:
     def  __init__( self ,name):
         self .name = name
     def  foo( self ):
         print ( 'hello %s'  %  self .name)
 
a = Student( 'Ethan' )
 
print (a.name)    # Ethan
print (a.foo())   # hello Ethan
 
print ( delattr (a, 'name' ))     # name属性被删除
print (a.name)    # 调用报错
Traceback (most recent call last):
   File  "D:/test.py" , line  50 in  <module>
     print (a.name)    # 调用报错
AttributeError:  'Student'  object  has no attribute  'name'
 
callable :检测对象是否可被调用
class  B:
     def  __call__( self * args,  * * kwargs):
         print ( 'instances are callable now' )
 
print ( callable (B))   # 类B是可调用对象
b = B()    # 调用类B
print ( callable (b))   # 实例b是可调用对象
print (b())   # 调用实例b成功
# instances are callable now

变量操作

1
2
globals :返回当前作用域内的全局变量和其值组成的字典
locals :返回当前作用域内的局部变量和其值组成的字典

交互操作

1
2
3
print :向标准输出对象打印输出
input :读取用户输入值
user = input ( 'please input your name:' )

文件操作

1
2
3
4
5
6
7
8
9
open :使用指定的模式和编码打开文件,返回文件读写对象
# 写入文件
a =  open ( 'a.text' , 'w' )
a.write( '124sdgadgahg ggadh' )
 
# 读取文件
a =  open ( 'a.text' , 'rt' )
print (a.read())
a.close()

编译执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
compile :将字符串编译为代码或者AST对象,使之能够通过 exec 语句来执行或者 eval 进行求值
# 流程语句使用exec
code1 = 'for i in range(5):print(i)'
compile1 = compile (code1,' ',' exec ')
exec  (compile1)
# 0
# 1
# 2
# 3
# 4
 
# 简单求值表达式用eval
code2 = '1+2+3+4'
compile2 = compile (code2,' ',' eval ')
print ( eval (compile2))    # 10
 
eval :执行动态表达式求值
print ( eval ( '1+2+3+4' ))   # 10
print ( eval ( '2*2*2' ))     # 8
print ( eval ( '10/2+2*2' ))  # 9.0
 
exec :执行动态语句块
exec  ( 'a=1+2' )
print (a)     # 3
exec  ( 'b=4*3/2-1' )
print (b)     # 5.0
 
repr :返回一个对象的字符串表现形式(给解释器)
a = 'hello world'
print ( str (a))    # hello world
print ( repr (a))   # 'hello world'

装饰器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
property :标示属性的装饰器
class  A:
     def  __init__( self ):
         pass
     @property
     def  foo( self ):
         print ( '1111111111' )
a = A()
print (a.foo)     # 访问属性,不需要加()执行foo
 
classmethod :标示方法为类方法的装饰器
class  B( object ):
     def  __init__( self ):
         pass
 
     @classmethod
     def  foo( cls ):
         print ( cls )
         
print (B.foo())   # 类对象调用类方法
# <class '__main__.B'>
b = B()
print (b.foo())   # 类实例对象调用类方法
# <class '__main__.B'>
 
staticmethod :标示方法为静态方法的装饰器
class  C( object ):
     def  __init__( self ):
         pass
     @staticmethod
     def  f1():
         print ( 'hahahha' )
         
print (C.f1())    # 类调用
c = C()
print (c.f1())    # 类实例对象调用

 补充:

复制代码
"""
python内置装饰器
在python中有三个内置的装饰器,都是跟class相关的:staticmethod、classmethod、property.
    @staticmethod 是类的静态方法,其跟成员方法的区别是没有self参数,并且可以在类不进行实例化的情况下调用
    @classmethod 与成员方法的区别在于所接收的第一个参数不是self(类实例的指针),而是cls(当前类的具体类型)
    @property 是属性的意思,表示可以通过类实例直接访问的信息
"""

class Foo(object):
    def __init__(self,var):
        super(Foo,self).__init__()
        self._var=var

    @property
    def var(self):
        return self._var

    @var.setter
    def var(self,var):
        self._var=var

f=Foo('var1')
print(f.var)
f.var='var2'
print(f.var)

"""
注意,对于Python新式类(new-style class),如果将上面的 “@var.setter” 装饰器所装饰的成员函数去掉,
则Foo.var 属性为只读属性,使用 “foo.var = ‘var 2′” 进行赋值时会抛出异常。
但是,对于Python classic class,所声明的属性不是 read-only的,所以即使去掉”@var.setter”装饰器也不会报错。
"""
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值