Python 字符串模块方法全解析(附带注释与案例)
1. 字符串基础操作Python 的字符串str是不可变序列提供了丰富的内置方法用于文本处理。以下汇总了字符串模块中的所有方法每个方法都附带注释说明和实用案例。2. 大小写转换方法2.1 str.capitalize()功能将字符串的第一个字符转换为大写其余字符转换为小写。案例text hello WORLD result text.capitalize() print(result) # 输出: Hello world2.2 str.title()功能返回标题化的字符串即每个单词的首字母大写其余字母小写。案例text python programming guide result text.title() print(result) # 输出: Python Programming Guide2.3 str.upper()功能将字符串中的所有字符转换为大写。案例text Hello Python result text.upper() print(result) # 输出: HELLO PYTHON2.4 str.lower()功能将字符串中的所有字符转换为小写。案例text HELLO PYTHON result text.lower() print(result) # 输出: hello python2.5 str.swapcase()功能将字符串中大写转换为小写小写转换为大写。案例text Hello World result text.swapcase() print(result) # 输出: hELLO wORLD2.6 str.casefold()功能返回字符串的 casefolded 副本用于无大小写比较比 lower() 更彻底。案例text1 Straße text2 STRASSE print(text1.casefold() text2.casefold()) # 输出: True3. 查找与替换方法3.1 str.find(sub[, start[, end]])功能返回子字符串 sub 在字符串中第一次出现的索引如果未找到则返回 -1。案例text hello world index text.find(world) print(index) # 输出: 63.2 str.rfind(sub[, start[, end]])功能返回子字符串 sub 在字符串中最后一次出现的索引如果未找到则返回 -1。案例text hello world, hello python index text.rfind(hello) print(index) # 输出: 133.3 str.index(sub[, start[, end]])功能与 find() 类似但如果未找到子字符串会引发 ValueError。案例text hello world try: index text.index(world) print(index) # 输出: 6 except ValueError: print(未找到子字符串)3.4 str.rindex(sub[, start[, end]])功能与 rfind() 类似但如果未找到子字符串会引发 ValueError。案例text hello world, hello python index text.rindex(hello) print(index) # 输出: 133.5 str.count(sub[, start[, end]])功能返回子字符串 sub 在字符串中出现的次数。案例text banana count text.count(na) print(count) # 输出: 23.6 str.replace(old, new[, count])功能返回字符串的副本其中所有出现的子字符串 old 都被替换为 new。案例text hello world new_text text.replace(world, Python) print(new_text) # 输出: hello Python4. 字符串判断方法4.1 str.startswith(prefix[, start[, end]])功能检查字符串是否以指定的前缀开头。案例text hello world result text.startswith(hello) print(result) # 输出: True4.2 str.endswith(suffix[, start[, end]])功能检查字符串是否以指定的后缀结尾。案例text hello.py result text.endswith(.py) print(result) # 输出: True4.3 str.isalnum()功能检查字符串是否只包含字母和数字。案例text1 Python3 text2 Python 3 print(text1.isalnum()) # 输出: True print(text2.isalnum()) # 输出: False包含空格4.4 str.isalpha()功能检查字符串是否只包含字母。案例text1 Python text2 Python3 print(text1.isalpha()) # 输出: True print(text2.isalpha()) # 输出: False包含数字4.5 str.isdigit()功能检查字符串是否只包含数字。案例text1 12345 text2 123.45 print(text1.isdigit()) # 输出: True print(text2.isdigit()) # 输出: False包含小数点4.6 str.isdecimal()功能检查字符串是否只包含十进制数字字符。案例text1 123 text2 ½ print(text1.isdecimal()) # 输出: True print(text2.isdecimal()) # 输出: False4.7 str.isnumeric()功能检查字符串是否只包含数字字符包括罗马数字、分数等。案例text1 123 text2 ½ text3 Ⅳ print(text1.isnumeric()) # 输出: True print(text2.isnumeric()) # 输出: True print(text3.isnumeric()) # 输出: True4.8 str.islower()功能检查字符串中所有字母字符是否都是小写。案例text1 hello text2 Hello print(text1.islower()) # 输出: True print(text2.islower()) # 输出: False4.9 str.isupper()功能检查字符串中所有字母字符是否都是大写。案例text1 HELLO text2 Hello print(text1.isupper()) # 输出: True print(text2.isupper()) # 输出: False4.10 str.isspace()功能检查字符串是否只包含空白字符。案例text1 text2 hello print(text1.isspace()) # 输出: True print(text2.isspace()) # 输出: False4.11 str.istitle()功能检查字符串是否为标题化字符串每个单词首字母大写。案例text1 Python Programming text2 python programming print(text1.istitle()) # 输出: True print(text2.istitle()) # 输出: False5. 字符串格式化方法5.1 str.format(*args, **kwargs)功能执行字符串格式化操作。案例name Alice age 25 text My name is {} and Im {} years old..format(name, age) print(text) # 输出: My name is Alice and Im 25 years old.5.2 str.format_map(mapping)功能使用映射对象执行字符串格式化。案例data {name: Bob, age: 30} text Name: {name}, Age: {age}.format_map(data) print(text) # 输出: Name: Bob, Age: 305.3 f-stringPython 3.6功能格式化字符串字面值在字符串前加 f 或 F使用 {} 包含表达式。案例name Charlie age 35 text fMy name is {name} and Im {age} years old. print(text) # 输出: My name is Charlie and Im 35 years old.6. 字符串分割与连接6.1 str.split(sepNone, maxsplit-1)功能使用分隔符 sep 分割字符串返回列表。案例text apple,banana,orange result text.split(,) print(result) # 输出: [apple, banana, orange]6.2 str.rsplit(sepNone, maxsplit-1)功能从右边开始分割字符串。案例text apple,banana,orange result text.rsplit(,, 1) print(result) # 输出: [apple,banana, orange]6.3 str.splitlines([keepends])功能按行分割字符串返回各行作为元素的列表。案例text Line 1\nLine 2\r\nLine 3 result text.splitlines() print(result) # 输出: [Line 1, Line 2, Line 3]6.4 str.partition(sep)功能在 sep 首次出现的位置分割字符串返回一个 3 元组。案例text hello-world-python result text.partition(-) print(result) # 输出: (hello, -, world-python)6.5 str.rpartition(sep)功能在 sep 最后一次出现的位置分割字符串返回一个 3 元组。案例text hello-world-python result text.rpartition(-) print(result) # 输出: (hello-world, -, python)6.6 str.join(iterable)功能将可迭代对象中的字符串元素连接成一个字符串。案例words [Python, is, awesome] result .join(words) print(result) # 输出: Python is awesome7. 字符串修剪与填充7.1 str.strip([chars])功能移除字符串开头和结尾的指定字符默认为空白字符。案例text hello world result text.strip() print(result) # 输出: hello world7.2 str.lstrip([chars])功能移除字符串开头的指定字符默认为空白字符。案例text hello world result text.lstrip() print(result) # 输出: hello world 7.3 str.rstrip([chars])功能移除字符串结尾的指定字符默认为空白字符。案例text hello world result text.rstrip() print(result) # 输出: hello world7.4 str.ljust(width[, fillchar])功能返回左对齐的字符串并使用 fillchar 填充至指定宽度。案例text hello result text.ljust(10, *) print(result) # 输出: hello*****7.5 str.rjust(width[, fillchar])功能返回右对齐的字符串并使用 fillchar 填充至指定宽度。案例text hello result text.rjust(10, *) print(result) # 输出: *****hello7.6 str.center(width[, fillchar])功能返回居中对齐的字符串并使用 fillchar 填充至指定宽度。案例text hello result text.center(11, *) print(result) # 输出: ***hello***7.7 str.zfill(width)功能返回指定宽度的字符串原字符串右对齐前面填充 0。案例text 42 result text.zfill(5) print(result) # 输出: 000428. 字符串编码与解码8.1 str.encode(encodingutf-8, errorsstrict)功能将字符串编码为 bytes 对象。

相关新闻

Java字节码修改与JAR重打包核心技术解析

Java字节码修改与JAR重打包核心技术解析

1. 字节码修改与JAR包重打包的核心原理字节码修改是Java生态中一项强大的技术能力,它允许开发者在编译后阶段直接操作.class文件内容。Java虚拟机(JVM)执行的不是原始Java代码,而是编译后生成的字节码指令集,这些指令以十六进制形式存储在.cl…

2026/7/18 3:29:09 阅读更多 →
Ionic项目打包安卓APK的完整指南

Ionic项目打包安卓APK的完整指南

1. Ionic项目打包安卓APK的前置准备在开始打包之前,我们需要确保开发环境已经正确配置。Ionic项目打包安卓APK主要有两种方式:使用Cordova或Capacitor。目前官方推荐使用Capacitor,因为它提供了更好的原生集成体验和更简单的配置流程。1.1 环…

2026/7/18 3:29:09 阅读更多 →
【Little Kernel】第1篇:LK中的自动注册机制

【Little Kernel】第1篇:LK中的自动注册机制

本文依据网络资料及工作与学习经验整理而成,如有错误请留言。 文章为付费内容,已加入原创侵权保护,禁止私自转载,违者必究。 文章所在专栏:《黑猫带你深入嵌入式系统:从裸机、U-Boot到内核与驱动》 看代码时候看到如下一段代码,有些看不懂,于是研究了下,是LK中自动注册…

2026/7/18 3:29:09 阅读更多 →

最新新闻

h4xx0r背后的故事:打造一个用于网络安全研究的开放平台

h4xx0r背后的故事:打造一个用于网络安全研究的开放平台

h4xx0r背后的故事:打造一个用于网络安全研究的开放平台 【免费下载链接】h4xx0r h4xx0r.org home page 项目地址: https://gitcode.com/gh_mirrors/h4/h4xx0r h4xx0r作为一个专注于网络安全研究的开放平台,其核心功能是通过实验性网页展示各类网络…

2026/7/18 7:37:55 阅读更多 →
STM32定时中断配置与应用全解析

STM32定时中断配置与应用全解析

1. 项目概述:STM32定时中断的核心价值定时中断是嵌入式系统开发中最基础也最重要的功能之一。在STM32单片机中,TIM(Timer)模块提供了强大的定时器功能,能够实现精确的时间控制和事件触发。通过配置TIM定时中断&#xf…

2026/7/18 7:37:55 阅读更多 →
FPGA驱动蜂鸣器实现音乐合成的原理与实现

FPGA驱动蜂鸣器实现音乐合成的原理与实现

1. 项目概述:FPGA驱动蜂鸣器的音乐合成原理在嵌入式系统开发中,用FPGA驱动蜂鸣器播放音乐是一个经典的入门项目。这个设计看似简单,却融合了数字逻辑设计、时序控制和音频合成的核心概念。我十年前第一次用Xilinx Spartan-6实现《欢乐颂》播放…

2026/7/18 7:37:55 阅读更多 →
Windows目录遍历不再踩坑:godirwalk跨平台兼容性深度测评

Windows目录遍历不再踩坑:godirwalk跨平台兼容性深度测评

Windows目录遍历不再踩坑:godirwalk跨平台兼容性深度测评 【免费下载链接】godirwalk Fast directory traversal for Golang 项目地址: https://gitcode.com/gh_mirrors/go/godirwalk 你是否曾因Go的filepath.Walk在Windows上表现异常而头疼?&…

2026/7/18 7:37:55 阅读更多 →
UE5肉鸽游戏开发:从ECS架构到程序化生成的实战指南

UE5肉鸽游戏开发:从ECS架构到程序化生成的实战指南

1. 项目概述:从零到一,用UE5打造你的第一款肉鸽游戏如果你是一名游戏开发者,或者对游戏制作充满热情,那么“肉鸽游戏”(Roguelike/Roguelite)这个品类绝对是你绕不开的经典。它以其高重复可玩性、随机生成的…

2026/7/18 7:37:54 阅读更多 →
Mac存储空间管理与应用卸载完全指南

Mac存储空间管理与应用卸载完全指南

1. Mac存储空间管理全攻略作为Mac用户,最让人头疼的问题之一就是存储空间不足。当系统提示"磁盘空间不足"时,很多人会感到手足无措。其实macOS提供了完善的存储管理工具,只是很多用户没有充分利用。1.1 查看存储空间使用情况最直接…

2026/7/18 7:36:54 阅读更多 →

日新闻

从模糊意图到可执行指令:Claude PRD中Prompt Engineering与需求颗粒度的5级映射法则

从模糊意图到可执行指令:Claude PRD中Prompt Engineering与需求颗粒度的5级映射法则

更多请点击: https://kaifayun.com 第一章:从模糊意图到可执行指令:Claude PRD中Prompt Engineering与需求颗粒度的5级映射法则 在Claude驱动的产品需求文档(PRD)生成实践中,原始业务意图往往以自然语言片…

2026/7/18 0:00:38 阅读更多 →
Cursor配置生成失效?3大隐藏陷阱+4行修复代码,资深工程师连夜整理的紧急补救清单

Cursor配置生成失效?3大隐藏陷阱+4行修复代码,资深工程师连夜整理的紧急补救清单

更多请点击: https://codechina.net 第一章:Cursor配置生成失效?3大隐藏陷阱4行修复代码,资深工程师连夜整理的紧急补救清单 Cursor 配置生成突然失效,是近期高频报障场景。表面看是 cursor.config.json 未更新或 LSP…

2026/7/18 0:00:38 阅读更多 →
某智驾大牛创业

某智驾大牛创业

作者:钟声编辑:Mark出品:红色星际头图:智能驾驶图片据悉,国内某头部智驾公司端到端模型技术大牛Z投身创业,并且已经拿到融资。Z不仅是该头部公司内部最年轻的对标阿里P10级别技术负责⼈,更是业内…

2026/7/18 0:00:38 阅读更多 →

周新闻

月新闻