目录
一、字符串的驻留机制
字符串——Python中的基本数据类型,不可变的字符序列
驻留机制:
仅保存一份相同且不可变字符串的方法.不同的值被存放在字符串的驻留池中,Python的驻留机制对相同的字符串只保留一份拷贝,后续创建相同字符串时,不会开辟新空间,而是把该字符串的地址赋给新创建的变量。
驻留机制的几种情况(交互模式)
1、字符串长度为0或1:
>>> s1 = ''
>>> s2 = ''
>>> s1 is s2
True
>>> s1 = '#'
>>> s2 = '#'
>>> s1 is s2
True
2、符合标识符的字符串(字母数字下划线):
>>> s1 = 'abcx'
>>> s2 = 'abcx'
>>> s1 is s2
True
>>> s1 = 'a_b'
>>> s2 = 'a_b'
>>> s1 is s2
True
>>> s1 = 'abc#@'
>>> s2 = 'abc#@'
>>> s1 == s2
True
>>> s1 is s2
False
3、字符串只在编译时进行驻留:
>>> a = 'abc'
>>> b = 'ab' + 'c'
>>> c = ''.join(['ab', 'c'])
>>> c
'abc'
>>> a is b
True
>>> a is c
False
4、[-5, 256]之间的整数数字:
>>> s1 = -6
>>> s2 = -6
>>> s1 is s2
False
>>> s1 = 4
>>> s2 = 4
>>> s1 is s2
True
intern方法
sys库中的intern方法,可以强制使2个字符串指向同一个对象
>>> import sys
>>> s1 = 'abc#@'
>>> s2 = 'abc#@'
>>> s1 is s2
False
>>> s1 = sys.intern(s2)
>>> s1 is s2
True
二、字符串的常用操作
1、字符串的查询
方法名称:
index():查找子串第一次出现的位置,如果查找的子串不存在,则抛出ValueError
find():查找子串第一次出现的位置,如果查找的子串不存在,则抛出-1
rindex():查找子串第一次出现的位置,如果查找的子串不存在,则抛出ValueError
rfind():查找子串第一次出现的位置,如果查找的子串不存在,则抛出-1
例如:
s = 'abcdefg'
print(s.index('c'))
print(s.find('c'))
print(s.rindex('d'))
print(s.rfind('d'))
print(s.index('k'))
print(s.find('k'))
print(s.rindex('k'))
print(s.rfind('k'))
>> 2
>> 2
>> 3
>> 3
>> ValueError: substring not found
>> -1
>> ValueError: substring not found
>> -1
2、字符串的大小写转换
方法名称:
upper():所有字符转大写
lower():所有字符转小写
swapcase():大写字符转小写,小写字符转大写
capitalize():将第一个字符转大写,其余字符转小写
title():将每个单词的第一个字符转为大写,其余转为小写
例如:
s = 'hellO WorlD'
print(s.upper()) # 转换后产生新的字符串对象
print(s.lower()) # 转换后产生新的字符串对象
print(s.swapcase())
print(s.capitalize())
print(s.title())
>> HELLO WORLD
>> hello world
>> HELLo wORLd
>> Hello world
>> Hello World
3、字符串的对齐
方法名称:
center():居中对齐,第1个参数指定宽度(小于字符串宽度则返回原字符串),第2个参数指定填 填充符(默认空格)
ljust():左对齐,第1个参数指定宽度(小于字符串宽度则返回原字符串),第2个参数指定填充符(默认空格)
rjust():右对齐,第1个参数指定宽度(小于字符串宽度则返回原字符串),第2个参数指定填充符(默认空格)
zfill():右对齐,仅接受一个参数,用于指定宽度(小于则返回原字符串),左边用0填充充
例如:
s = 'hellO WorlD'
print(s.center(20, '*'))
print(s.ljust(20, '*'))
print(s.rjust(20, '*'))
print(s.zfill(20))
>> ****hellO WorlD*****
>> hellO WorlD*********
>> *********hellO WorlD
>> 000000000hellO WorlD
4、字符串的“劈分”
方法名称:
split():从左边开始劈分,默认的劈分字符是空格,返回值是列表。可指定劈分符(sep=),及劈分次数(maxsplit=)
rsplit():从右边开始劈分,默认的劈分字符是空格,返回值是列表。可指定劈分符(sep=),及劈分次数(maxsplit=)
例如:
s1 = 'hello world python'
s2 = 'hello|world|python'
print(s1.split())
print(s2.split('|'))
print(s2.split(sep='|', maxsplit=1))
print(s1.rsplit())
print(s2.rsplit('|'))
print(s2.rsplit(sep='|', maxsplit=1))
>> ['hello', 'world', 'python']
>> ['hello', 'world', 'python']
>> ['hello', 'world|python']
>> ['hello', 'world', 'python']
>> ['hello', 'world', 'python']
>> ['hello|world', 'python']
5、字符串的判断
方法名称:
isidentifier():判断指定的字符串是不是合法的标识符
isspace():判断指定的字符串是否全部由空白字符(回车、换行、制表符)组成
isalpha():判断指定的字符串是否全为字母
isdecimal():判断指定的字符串是否全由十进制数字组成
isnumeric():判断指定的字符串是否全由数字组成
isalnum():判断指定的字符串是否全由字母和数字组成
6、其他
字符串替换
replace():第1个参数指定被替换的子串,第2个参数指定替换子串的字符串,可通过第3个参数指定最大替换次数
s1 = 'hello,python'
s2 = 'hello,python,python,python'
print(s1.replace('python', 'c++'))
print(s2.replace('python', 'c++', 2))
>> hello,c++
>> hello,c++,c++,python
字符串合并
join():将列表或元祖中的字符串合并为一个字符串
lst = ['hello', 'world', 'python']
print(' '.join(lst))
print(','.join(lst))
>> hello world python
>> hello,world,python
三、字符串的比较
运算符:>,>=,<,<=,==,!=
比较规则:
首先比较两个字符串的第一个字符,如果相等则继续比较下一对字符,一次比较下去,直至不相等,此时比较的结果则为两个字符串的比较结果。后续字符不再比较。
注意:
== 比较的是 value
is 比较的是 id
四、字符串的切片
切记:字符串是不可变类型,其不具备增、删、改等操作。切片操作将产生新的对象
先来看下索引,py的索引可顺可逆
例如:
s1 = 'hello,python'
s2 = 'hello,python,python,python'
print(s1[1:6])
print(s2[-6:-1])
>> ello,
>> pytho
五、格式化字符串
方法一:
%作占位符
name = 'cheems'
age = 6
print('我的狗狗叫:%s,它今年%d岁了' % (name, age))
>> 我的狗狗叫:cheems,它今年6岁了
print('%5d' % 99) # 宽度为5
print('%.2f' % 3.1415926) # 保留两位小数
print('%10.2f' % 3.1415)
>> 99
>> 3.14
>> 3.14
方法二:
{}作占位符
name = 'cheems'
age = 6
print('我的狗狗叫:{0},它今年{1}岁了,是的,叫{0}'.format(name, age))
>> 我的狗狗叫:cheems,它今年6岁了,是的,叫cheems
print('{0:.2}'.format(3.1415926)) # .2表示一共2位数
print('{0:.2f}'.format(3.1415926)) # .2f表示2位小数
print('{0:10.2f}'.format(3.1415926)) # 同时设置,一共10位数,3位是小数
>> 3.1
>> 3.14
>> 3.14