python字符串不可变数据类型

python字符串不可变数据类型

目录

一、截取子串-切片

二、查找子串及数量

三、字符串的替换、分割以及合并

四、字母的大小写

五、删除侧边的空白

六、对齐方式

七、判断首位字符是否正确(返回布尔值)

八、判断字母、数以及空格

一、截取子串-切片

方法:字符串名[初始位置:结束位置:步长]

str1 = 'abcdefg' print(str1[:]) # abcdefg print(str1[0:2]) # ab 左闭右开 print(str1[0:2:1]) # ab 默认步长为1 print(str1[:3]) # abc print(str1[0:]) # abcdefg print(str1[::-1]) # gfedcba print(str1[-4:-1]) #def -1代表最右端 # print(str1[-4:-1:-1]) # 尽量避免这样写 二、查找子串及数量

方法:

1. 字符串名.index(子串,起始位置,末位置) ---对应 rindex 从右往左数

2. 字符串名.find(子串,起始位置,末位置)   --对应rfind从右往左数

3. 字符串名.count(子串,其实位置,末位置)

str1 = 'my name is fl and my pet is a parrot' print(str1.find('is')) # 8 print(str1.find('is', 9, 30)) # 25 print(str1.find('dog')) # -1 --find如果没有查找到对应的子串则返回-1 print(str1.index('is')) # 8 print(str1.index('is', 9, 30)) # 25 # print(str1.index('dog')) # -1 --index如果没有查找到对应的子串则报错 print(str1.rindex('is')) # 25 print(str1.rfind('is')) # 25 print(str1.count('is')) # 2 print(str1.count('dog')) # 0 print(str1.count('is', 0, 15)) # 1 三、字符串的替换、分割以及合并

1. 字符串的替换:字符串名.replace(原串,替换成的串, 替换次数)

str1 = 'Food and Fruit and Animal' new_str = str1.replace('and', 'he') # Food he Fruit he Animal new_str = str1.replace('and', 'he', 1) # Food he Fruit and Animal

2. 字符串的分割:字符串名.split(分割的串, 分割次数)

str1 = 'Food and Fruit and Animal' new_str = str1.split('and') # ['Food ', ' Fruit ', ' Animal'] new_str = str1.split('and', 1) # ['Food ', ' Fruit and Animal']

3. 字符串的合并:合并符号.join(列表名)

mylist = ['I', 'Love', 'You'] new_str = ' '.join(mylist) # I Love You new_str = '..'.join(mylist) # I..Love..You 四、字母的大小写

1. 仅首字母大写:字符串名.capitalize()

2. 每个单词的首字母都变成大写:字符串名.title()

3. 将每个小写字母都变成大写字母:字符串名.upper()

4. 将每个大写字母都变成小写字母:字符串名.lower()

str1 = 'oh , my dear , I love you' new_str = str1.capitalize() # Oh , my dear , i love you new_str = str1.title() # Oh , My Dear , I Love You new_str = str1.upper() # OH , MY DEAR , I LOVE YOU new_str = str1.lower() # oh , my dear , i love you 五、删除侧边的空白

1. 删除左侧空白:字符串名.lstrip()

2. 删除右侧空白:字符串名.rstrip()

3. 删除两侧空白:字符串名.strip()

str1 = "   come on baby , it's true  " new_str = str1.lstrip() # come on baby , it's true new_str = str1.rstrip() #    come on baby , it's true new_str = str1.strip() # come on baby , it's true 六、对齐方式

方法:字符串名.ljust(整体字符长度, 用于填补的符号)

str1 = 'abcd' new_str = str1.ljust(10, '.') # abcd...... new_str = str1.ljust(10) # abcd(后面有六个空格) new_str = str1.rjust(10, '/') # //abcd new_str = str1.center(10, '.') # ...abcd... 七、判断首位字符是否正确(返回布尔值)

1. 字符串名.startswith(子串, 起始位置, 末位置)

2. 字符串名.endswith(子串, 起始位置, 末位置)

str1 = 'hello world' print(str1.startswith('he')) # True print(str1.startswith('ho')) # False print(str1.endswith('ho')) # False print(str1.endswith('world')) # True 八、判断字母、数以及空格

1. 判断是否所有都为字母:字符串名.isalpha()

2. 判断是否所有都为数字:字符串名.isdigit()

3. 判断是否所有都为字母或数字或其组合:字符串名.isalnum()

4. 判断是否所有都为空格:字符串名.isspace()

str1 = 'hello world' print(str1.isalpha()) # False str1 = 'abc' print(str1.isalpha()) # True str2 = '123 45' print(str2.isdigit()) # False str2 = '123' print(str2.isdigit()) # True str3 = 'abc123' print(str3.isalnum()) # True str3 = 'abc 123' print(str3.isalnum()) # False str4 = '123 ' print(str4.isspace()) # False str4 = '   ' print(str4.isspace()) # True

到此这篇关于python字符串不可变数据类型的文章就介绍到这了,更多相关python字符串内容请搜索易知道(ezd.cc)以前的文章或继续浏览下面的相关文章希望大家以后多多支持易知道(ezd.cc)!

推荐阅读