Python字符串类型

Python字符串类型

1.创建

s = 'Hello,man!How are you!'

特性

  • 有序

  • 不可变

    不可变如何体现,看代码:

    a = 'blue'
    print(id(a))  # 输出结果 34390568
    a = 'pink'
    print(id(a))  # 输出结果 41910600
    

    当给a二次复制的时候,并未更改 'blue',而是重新开辟了内存空间存放 'pink'

2.字符串方法

如何进入到源码查看,随便写一个字符串方法,Ctrl + 鼠标左击,然后找到str 的方法,如下图

这是C语言写的,所以我们并不会看到python的真正源码是如何实现的,只能看到解释

01 capitalize() 首字母大写,其他字母小写
>>> a = 'Hello world'
>>> a.capitalize()
'Hello world'
02 casefold() 全部转化为小写

对于字符串比较的时候,可以全部转化为小写然后比较

>>> a = 'Hello world'
>>> a.casefold()
'hello world'
03 center() Return S centered in a string of length width. Padding isdone using the specified fill character (default is a space)
>>> a
'Hello world'
>>> a.center(50,'*')
'*******************Hello world********************'
04 count() 计算包含多少个某字符
>>> a = 'Hello world'
>>> a.count('o')
2
>>> a.count('l',0,3) #从0到3,包含多少个 l, 区间[H,e,l,l)
1
05 endswith() 判断是不是以 什么 结尾,返回Boolean
>>> a = 'Hello world!'
>>> a.endswith('!')
True
>>> a.endswith('d!')
True
>>> a.endswith('d')
False
06 expandtabs() 增加tabs长度的
>>> a = 'a	b' #	表示一个tab
>>> print(a)
a       b
>>> a.expandtabs(20)
'a                   b'
07 find() 查找,找到返回index,找不到返回-1
>>> a = 'Hello world!'
>>> a.find('o') #从左到右,返回找到的first 目标原素就返回
4
>>> a.find('k')
-1
>>> a.find('l',0,4) 
2
08 format() 返回一个新字符串,不改变原字符串
'I am {0},and I like {1}'
>>> str.format('gudon','apple')
'I am gudon,and I like apple'
>>> print(str)
I am {0},and I like {1}
>>> str = 'I am {name},and I like {fruts}'
>>> str.format(name='jack',fruts='orange')
'I am jack,and I like orange'
09 isdecimal() 法检查字符串是否只包含十进制字符 ,这种方法只存在于unicode对象。

注意:定义一个十进制字符串,只需要在字符串前添加 'u' 前缀即可。

>>> str = u'this2018'
>>> str.isdecimal()
False
>>> str = u'2018'
>>> str.isdecimal()
True
10 isdigit() 方法检测字符串是否只由数字组成。
>>> '2018'.isdigit()
True
>>> 'hello'.isdigit()
False
11 isidentifier()判断是否为Python中的标识符
>>> '33a'.isidentifier()
False
>>> '_name'.isidentifier()
True
12 islower() 判断是否都是小写 ,相对与 isupper()
#islower()
>>> '33abc'.islower()
True
>>> '33abDc'.islower()
False
# isupper()
>>> 'ASD'.isupper()
True
>>> 'ASDaa'.isupper()
False
13 isnumeric() 如果字符串中只包含数字字符,则返回 True,否则返回 False
str = u"this2009";  
print str.isnumeric(); #False

str = u"23443434";
print str.isnumeric(); #True

#对于 Unicode 数字、全角数字(双字节)、罗马数字和汉字数字会返回 True ,其他会返回 False。byte数字(单字节)无此方法。
print u'123'.isnumeric() # True
print u'Ⅷ'.isnumeric() # True
print u'abc123'.isnumeric() # False
print u'1.23'.isnumeric() # False
>>> '五'.isnumeric() #True

num = "1"  #unicode
num.isdigit()   # True
num.isdecimal() # True
num.isnumeric() # True

num = "1" # 全角
num.isdigit()   # True
num.isdecimal() # True
num.isnumeric() # True

num = b"1" # byte
num.isdigit()   # True
num.isdecimal() # AttributeError 'bytes' object has no attribute 'isdecimal'
num.isnumeric() # AttributeError 'bytes' object has no attribute 'isnumeric'

num = "IV" # 罗马数字
num.isdigit()   # True
num.isdecimal() # False
num.isnumeric() # True

num = "四" # 汉字
num.isdigit()   # False
num.isdecimal() # False
num.isnumeric() # True

14 join()
>()>> names = ['gudon','jack','fesco']
>>> names
['gudon', 'jack', 'fesco']
>>> ''.join(names)
'gudonjackfesco'
>>> '+'.join(names)
'gudon+jack+fesco'
15 lower() and upper() 转换大小写
>>> s.lower()
'hello world'
>>> s.upper()
'HELLO WORLD'
16 Python ljust() 方法返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
>>> s = 'Hello world'
>>> s.ljust(50)
'Hello world                                       '
>>> s.ljust(50,'*')
'Hello world***************************************'
>>> s.ljust(5)
'Hello world'
17 strip() 去除空格等 ,lstrip()只去除左边 rstrip()只去除右边
>>> s = '
 hello world     '
>>> s
'
 hello world     '
>>> s.strip()
'hello world'
18 replace() 字符串替换
>>> s = 'Hello world'
>>> s.replace('l','L')
'HeLLo worLd'
>>> s.replace('l','L',2)
'HeLLo world'
19 partition() 根据传入的指定分隔符,返回一个3元的元祖,第一个为左边字符串,第二个为分隔符本身,第三个为分隔符右边字符串,对应的有 rpartition(),从右边开始分
>>> s = 'www.fesco.com.cn'
>>> s.partition('.')
('www', '.', 'fesco.com.cn')
>>> s.rpartition('.')
('www.fesco.com', '.', 'cn')
20 split() 通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串 ,从右边开始分,rsplit()
>>> str = 'this is an apple'
>>> str.split() # 分隔符,默认为所有的空字符,包括空格、换行(
)、制表符(	)等
['this', 'is', 'an', 'apple']
>>> str.split('a')
['this is ', 'n ', 'pple']
>>> str.split('i',1)
['th', 's is an apple']
21 startswith() and endswith()
>>> str = 'Hello world!'
>>> str.startswith('H')
True
>>> str.endswith('d!')
True
22 swapcase() 大小写切换,原来是大写,变成小写,原来是小写的,变成大写
>>> str = 'Hello world!'
>>> str.swapcase()
'hELLO WORLD!'
原文地址:https://www.cnblogs.com/friday69/p/9143113.html