字符串函数相关函数

本文详细介绍了Python中字符串的各种操作方法,包括capitalize、casefold、center、count、endswith等,涵盖了字符串的格式化、查找、替换、切割、填充等功能,是Python字符串处理的重要参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

"""
我于黑暗中蛰伏,只为抓住黎明一瞬
Author:huihan
Time:2022/2/27 20:46
"""
# str-字符串
# 1.capitalize-第一个字母大写
nums='jjj'
print(nums.capitalize())
# 2.casefold-将大写字母变成小写字母
nums='jjjASD'
print(nums.casefold())
# 3.center-返回长度为width的居中字符串。填充使用指定的填充字符(默认是空格)。
nums='jjjASD'
result=nums.center(8)#总长度为8,前面一个空格,后面一个空格
print(result)
# 4. count  - 统计个数
# 字符串1.count(字符串2)   -  统计字符串1中字符串2出现的次数
str1 = 'how are you? i am fine, thank you! and you?'
print(str1.count('o'))
print(str1.count('you'))
# 5.endswith判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回 True,否则返回 False。
str1 = 'how are you? i am fine, thank you! and you?'
print(str1.endswith('you?'))
# 6. expandtabs-把字符串 string 中的 tab 符号转为空格,tab 符号默认的空格数是 4
str = "runoob\t12345\tabc"
print('原始字符串:', str)
# 7.find-如果包含子字符串返回开始的索引值,否则返回-1
str1 = 'how are you? i am fine, thank you! and you?'
print(str1.find('you'))
print(str1.rfind('you'))
# 8.format-格式化字符串的函数 str.format(),它增强了字符串格式化的功能。将各个下标的字符串重新排序
print("{2} {0}".format("0","6","5"))
print("{0} {1}".format("hello", "world"))
# 9.format_map-针对字典类型的格式化后的新的对象
People = {"name": "john", "age": 33}
print("My name is {name},iam{age} old".format_map(People))
# 10.index-如果包含子字符串返回开始的索引值,不在会报错
print(str1.index('you'))
# print(str1.rindex('wo'))
# 11.isalnum-数字字符: 字符的意义是表示一个数值的字符是True,不能识别汉字数字
print(1111111111)
print('234一十百万千ⅠⅤ壹'.isalnum())
# 12.isalpha-如果字符串至少有一个字符并且所有字符都是字母或者汉字则返回 True,否则返回 False
str = "runoob菜鸟教程"
print(str.isalpha())
# 13.isdecimal-方法检查字符串是否只包含十进制字符
str = u"this2009"
print(str.isdecimal())
# 14.isdigit-判断是否是纯数字字符串
print('234一'.isdigit())
# 15.isidentifier-用于判断字符串是否是有效的 Python 标识符,可用来判断变量名是否合法。
print( "123".isidentifier() )
# 16.islower-字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False
print('jhui'.islower())
# 17.isnumeric  -  数字字符: 字符的意义是表示一个数值的字符
print('234一十百万千ⅠⅤ壹'.isnumeric())
# 18.isprintable-检查文本中的所有字符是否可打印:
txt = "Hello!\nAre you #1?"
x = txt.isprintable()
print(x)
# 19.isspace-方法检测字符串是否只由空白字符组成。
str = "       "
print (str.isspace())
# 20.istitle-检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。
str = "This Is String Example...Wow!!!"
print (str.istitle())
# 21.isupper-检测字符串中所有的字母是否都为大写。
str = "THIS is string example....wow!!!"
print (str.isupper())
# 22.join
# 字符串.join(序列)  -  将序列中的元素用指定字符串拼接处一个字符串(序列中元素必须是字符串)
list1 = ['name', 'age', 'gender']
result = '+'.join(list1)
print(result)   # 'name+age+gender'
# 23.ljust-返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
str = "Runoob example....wow!!!"
print (str.ljust(50, '*'))
# 24.lower-转换字符串中所有大写字符为小写.
str = "Runoob example....wow!!!"
print(str.lower())
# 25.lstrip-用于截掉字符串左边的空格或指定字符。
str = "88888888this is string example....wow!!!8888888";
print( str.lstrip('8') );
# 26.maketrans-str.maketrans(字符串1, 字符串2)  -  创建字符串1中所有字符和字符串2中所有字符一一对映关系表
# 字符串.translate(字符对映表)   -   按照字符対映表的关系将字符串中的字符进行替换
str1 = 'abnsmahsjdbmnsdxybksdall'
table = str.maketrans('ab', '12')
result = str1.translate(table)
print(result)
# 27.partition-partition() 方法搜索指定的字符串,并将该字符串拆分为包含三个元素的元组。第一个元素包含指定字符串之前的部分。第二个元素包含指定的字符串。第三个元素包含字符串后面的部分。
# 如果找不到指定的值,则 partition() 方法将返回一个元组,其中包含:1 - 整个字符串,2 - 空字符串,3 - 空字符串:
txt = "I could eat bananas all day"
x = txt.partition("apples")
print(x)
# 28.replace-替换
#  字符串1.replace(字符串2, 字符串3)   -  将字符串1中所有的字符串2都替换成字符串3
# 字符串1.replace(字符串2, 字符串3, N)   -   将字符串1中前N个字符串2都替换成字符串3
str1 = 'how are you? i am fine, thank you! and you?'
result = str1.replace('you', 'me')
print(result)       # 'how are me? i am fine, thank me! and me?'
# 29.rfind-在字符串中搜索指定的值,并返回它被找到的最后位置。
str1 = 'how are you? i am fine, thank you! and you?'
print(str1.rfind('you'))
# 30.rindex()-在字符串中搜索指定的值,并返回它被找到的最后位置。没有会报错
str1 = 'how are you? i am fine, thank you! and you?'
# print(str1.rindex('yu'))
# 31.rjust-返回字符串的右对齐版本。
str1 = 'how are you? i am fine, thank you! and you?'
print(str1.rjust(60,'/'))
# 32.rpartition-方法搜索指定的字符串,并将该字符串拆分为包含三个元素的元组。第一个元素包含指定字符串之前的部分。第二个元素包含指定的字符串。第三个元素包含字符串后面的部分。
# # 如果找不到指定的值,则 partition() 方法将返回一个元组,其中包含:1 - 整个字符串,2 - 空字符串,3 - 空字符串:(从右边开始)
txt = "I could eat bananas all day, bananas are my favorite fruit"
x = txt.rpartition('a')
print(x)
# 33.rsplit-字符串1.rsplit(字符串2, N)   -  将字符串1倒数前N个字符串2作为切割点对字符串进行切割
str1 = '123**abc*mn'
result = str1.rsplit('*', 5)
print(result)
# 34.rstrip-rstrip() 方法删除所有结尾字符(字符串末尾的字符),空格是要删除的默认结尾字符。
txt = "banana,,,,,ssaaww....."
x = txt.rstrip(",.aws")
print(x)
# 35.split
# 字符串1.split(字符串2)    -   将字符串1中所有的字符串2作为切割点对字符串进行切割
# # 字符串1.split(字符串2, N)    -  将字符串1中前N个字符串2作为切割点对字符串进行切割
str1 = '123*abc*mn'
result = str1.split('*')
print(result)    # ['123', 'abc', 'mn']
# 注意1:如果切割点在字符串的开头或者结尾,切完后会出现空串
str1 = '*123*abc*mn*'
result = str1.split('*')
print(result)       # ['', '123', 'abc', 'mn', '']
# 注意2:如果切割点连续出现,切完后也会出现空串
str1 = '123**abc*mn'
result = str1.split('*')
print(result)       # ['123', '', 'abc', 'mn']
str1 = '123*abc*mn*xyz*你好'
result = str1.split('*', 2)
print(result)
# 36.splitlines-在换行符处拆分字符串并返回列表。
txt = "Thank you for your visiting\nWelcome to China"
x = txt.splitlines()
print(x)
# 37.startswith-如果以指定值开头的字符串,则返回 true。
txt = "Hello, welcome to my world."
x = txt.startswith("Hello")
print(x)
# 38.strip-删除字符串两端的空白
# 字符串.strip()   -  删除字符串两边的空白
# 字符串.rstrip()  -   删除字符串右边的空白
# 字符串.lstrip()   -   删除字符串左边的空白
str1 = '\t     \n   a b    c   \n\n'
print(str1)
print('============================')
print(str1.strip())
print('============================')
print(str1.lstrip())
# 39.swapcase-将小写字母大写,大写字母小写:
txt = "Hello My Name Is Elon"
x = txt.swapcase()
print(x)
# 40.title-将每个单词的首字母大写:
txt = "Welcome to my world"
x = txt.title()
print(x)
# 41.upper-方法返回一个字符串,其中所有字符均大写
txt = "Hello my friends"
x = txt.upper()
print(x)
# 42.zfill-用零填充字符串,直到长度为 10 个字符:
txt = "50"
x = txt.zfill(5)
print(x)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值