当前位置:主页 > 软件编程 > Python代码 >

Python处理字符串的常用函数实例总结

时间:2022-11-07 09:37:33 | 栏目:Python代码 | 点击:

前言

今天我们说了字符串的基础,格式化,这次我们讲解字符串的常用函数,不要错过!

前两篇都在本文同个专栏,欢迎关注。下面开始讲解。

 字符串都有哪些操作?

实际开发都有这些需求:

第一大类:判断识别字符串

第二类:字符串编辑的操作(生成新字符串)

第三类:字符串跟字节串的互转。

这类操作通常发生在数据跨程序/跨服务器传输,我们传输bytes,然后获取转string类型。

第一类 判断识别字符串

学委准备了下面的代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/30 10:13 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : string_funs_cat1.py
# @Project : hello
import sys

slogan = "keep studying, keep coding, I am Levin"

# 判断结构
print("算某个子串出现数次: slogan.count('keep') = ", slogan.count('keep'))
print("找某个子串首次出现的下标: slogan.find('keep') = ", slogan.find('keep'))
print("找某个子串最后出现的下标: slogan.rfind('keep') = ", slogan.rfind('keep'))
print("找某个子串下标: slogan.index('keep') = ", slogan.index('keep'))
print("找某个子串下标: slogan.rindex('keep') = ", slogan.rindex('keep'))
print("是否'keep'开头的字符串: slogan.startswith('keep') = ", slogan.startswith('keep'))
print("是否'keep'结束的字符串: slogan.endswith('keep') = ", slogan.endswith('keep'))


# 字符串属性相关
print("字符串长度: len(slogan) = ", len(slogan))
print("字符串是否都是空格: slogan.isspace() = ", slogan.isspace())
print("字符串是否大写: slogan.isupper() = ", slogan.isupper())
print("字符串是否小写: slogan.islower() = ", slogan.islower())
print("字符串是否为每个词首字母都大写: slogan.istitle() = ", slogan.istitle())

# 判断字符串数据类型
print("字符串是否全为字母: slogan.isalpha() = ", slogan.isalpha())
print("字符串是否全为数字: slogan.isalnum() = ", slogan.isalnum())
print("字符串是否数字: slogan.isnumeric() = ", slogan.isnumeric())
print("字符串是否浮点数: slogan.isdecimal() = ", slogan.isdecimal())
print("字符串是否为空格串: slogan.isspace() = ", slogan.isspace())

读者可以直接复制运行代码,学委补充了运行效果图:

第二类 字符串编辑的操作

下面学委准备了一些代码展示:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/30 10:13 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : string_funs_cat2.py
# @Project : hello
import sys

slogan = "keep studying, keep coding, I am Levin"

print("首字母大写: slogan.capitalize() = ", slogan.capitalize())
print("全部字母大写: slogan.upper() = ", slogan.upper())
print("全部字母小写: slogan.lower() = ", slogan.lower())
print("转为首字母都大写(标题风格): slogan.title() = ", slogan.title())
print("大小写逆转: slogan.swapcase() = ", slogan.swapcase())

table = slogan.maketrans({"e": "5"})
print("字符串替换表: slogan.translate(table) = ", slogan.translate(table))

# 字符串替换,合并,填充等
print("替换tabs为n个空格: 'hello\t学委'.expandtabs(4) = '", "hello\t学委".expandtabs(4))
print("左子串来串联传入的列表: ' '.join(slogan) = '", " ".join(slogan))
print("替换子串: ' '.replace(first, second) = '", slogan.replace("e", "11"))
print("填充0值: slogan.rzfill(2)= '", slogan.zfill(50))
print("填充#值: slogan.rjust(50,"#")= '", slogan.rjust(50,"#"))
print("填充#值: slogan.ljust(50,"#")= '", slogan.ljust(50,"#"))
print("移除首尾空格: slogan.strip()= '", slogan.strip())

data = slogan.split("e")
print("split slogan into data= ", data)

运行效果如下:

第三类:字符串跟字节串的互转

下面学委准备了一些代码展示:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/30 10:13 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : string_funs_cat3.py
# @Project : hello
import sys

slogan = "keep studying, keep coding, I am Levin"
bytes = slogan.encode("utf-8")
print("type of encoded string = ", type(bytes))
# 注意python的string类型没有decode函数,该函数属于bytes类型对象特有!!!
print("type of decoded byte = ", type(bytes.decode("utf-8")))
print("type of decoded byte = ", bytes.decode("utf-8"))

运行效果如下:

总结

其实学委还漏了几个函数,但是不想介绍它们了。

学习编程不是去记忆,但是也并非啥都不看都靠感觉。

我喜欢下面这句话:

读书破万卷,下笔如有神!

精心准备的代码,读者运行一下,自己感悟!(注释写的很清楚了)

您可能感兴趣的文章:

相关文章