python字符串内置函数总结

本文全面介绍了Python中字符串的各种操作方法,包括大小写转换、字符类型判断、字符串替换、空格处理、连接与分割、搜索等功能,并详细解释了isdigit、isdecimal、isnumeric的区别。

字符串内置总结

需要注意的是:

  • 字符串的单引号和双引号都无法取消特殊字符的含义,如果想让引号内所有字符均取消特殊意义,在引号前面加r,如name=r’l\thf’
  • unicode字符串与r连用必需在r前面,如name=ur’l\thf’

大小写处理

函数作用示例输出
capitalize首字母大写,其余小写‘lk with psr’.capitalize()‘Lk with psr’
upper全部大写‘lk with psr’.upper()‘LK WITH PSR’
lower全部小写‘lk with psr’.lower()‘lk with psr’
swapcase()大小写互换‘Lk with Psr’.swapcase()‘lK WITH pSR’
.title()首字母大写‘lk with psr’.title()‘Lk With Psr’

判断字符串中的字符类型

函数作用示例输出
startswith(‘a’,[start,end])是否以a开头‘a lk’.startswith(‘a’)True
endswith(‘a’)是否以a结尾‘lk’.endswith(‘a’)False
isalnum()是否全为字母或数字‘123asd’.isalnum()True
isalpha()是否全字母‘lk12’.isalpha()True
isdigit()是否全数字‘123’.isdigit()True
islower()是否全小写‘lk’.islower()True
isupper()是否全大写‘lk’.isupper()False
istitle()判断首字母是否为大写‘Lk’.istitle()True
isspace()判断字符是否为空格’ '.isspace()True

字符串替换

函数作用示例输出
replace(‘old’,‘new’)替换old为new’hello world’.replace(‘world’,‘python’)hello python
replace(‘old’,‘new’,次数)替换指定次数的old为new’hello world’.replace(‘l’,‘p’,2)heppo world

去空格

函数作用示例输出
strip()去两边空格’ h e-l lo '.strip()可以想象
lstrip()去左边空格’ h e-l lo '.lstrip()可以想象
rstrip()去右边空格’ h e-l lo '.rstrip()可以想象

用特定符连接单个字符

函数作用示例输出
.join()-连接‘-’.join([‘a’, ‘b’, ‘c’])a-b-c

用字符串中的特定符分割字符串

函数作用示例输出
split()默认按空格分隔’ h e-l lo ’ .split()[‘h’, ‘e-l’, ‘lo’]
split(‘指定字符’)按指定字符分割字符串为数组’ h e-l lo ’ .split(’-’)[’ h e’, 'l lo ']

搜索

函数作用示例输出
find()搜索指定字符串,没有返回-1‘lk la’.find(‘lk’)0
index()同上,但是找不到会报错‘lk la’.index(‘lk’)0
rfind()从右边开始查找‘lk la’.rfind(‘lk’)0
count()统计指定的字符串出现的次数‘lklklk’.count(‘lk’)3

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

isdigit()
True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字
False: 汉字数字
Error: 无
 
isdecimal()
True: Unicode数字,,全角数字(双字节)
False: 罗马数字,汉字数字
Error: byte数字(单字节)
 
isnumeric()
True: Unicode数字,全角数字(双字节),罗马数字,汉字数字
False: 无
Error: byte数字(单字节)
### Python 字符串内置函数 Python 提供了一组丰富的字符串处理方法,这些方法可以直接应用于字符串对象。以下是常用的一些字符串内置函数及其说明: #### `str.upper()` 此方法返回原字符串的大写版本。 ```python text = "hello world" print(text.upper()) # 输出 HELLO WORLD ``` [^1] #### `str.lower()` 该方法用于获取字符串的小写形式。 ```python text = "HELLO WORLD" print(text.lower()) # 输出 hello world ``` #### `str.strip([chars])` 移除字符串开头和结尾指定的字符,默认为空白字符(包括空格、制表符等)。如果提供参数,则会删除匹配的前缀或后缀字符。 ```python text_with_spaces = " remove spaces " cleaned_text = text_with_spaces.strip() print(cleaned_text) # 输出 'remove spaces' ``` #### `str.split(sep=None)` 按照给定分隔符分割字符串并返回列表;如果不给出分隔符则默认按任意空白字符作为分界线。 ```python sentence = "split this sentence into words" words_list = sentence.split() print(words_list) # 输出 ['split', 'this', 'sentence', 'into', 'words'] ``` #### `str.join(iterable)` 将可迭代对象中的元素连接成一个新的字符串,各元素之间用调用者字符串间隔开。 ```python list_of_words = ["join", "these", "words"] joined_string = "-".join(list_of_words) print(joined_string) # 输出 join-these-words ``` #### `str.replace(old, new[, count])` 替换旧子串`old`为新子串`new`; 如果指定了次数`count`, 则最多只替换成指定数量次. ```python original_sentence = "replace all occurrences of word" modified_sentence = original_sentence.replace("word", "phrase") print(modified_sentence) # 输出 replace all occurrences of phrase ``` 更多关于字符串操作的方法可以在官方文档的数据模型章节找到详细的描述。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UESTC Like_czw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值