@@ -665,6 +665,29 @@ Python 字符串提供了强大而直观的文本处理能力:
665665- ` replace() ` 默认全局替换,无需 ` replaceAll() `
666666- ` " | ".join(list) ` 连接方式与 ` array.join() ` 相反
667667
668+ ** 常用方法速查表** :
669+
670+ | 方法 | 描述 | 示例 |
671+ | ------| ------| ------|
672+ | ` len(text) ` | 获取字符串长度(函数调用) | ` len("hello") ` → ` 5 ` |
673+ | ` text[index] ` | 索引访问字符(支持负索引) | ` "hello"[1] ` → ` 'e' ` |
674+ | ` text[start:end] ` | 切片操作(左闭右开) | ` "hello"[1:4] ` → ` 'ell' ` |
675+ | ` "sub" in text ` | 检查子字符串 | ` "ll" in "hello" ` → ` True ` |
676+ | ` text.upper() ` | 转为大写 | ` "hello".upper() ` → ` 'HELLO' ` |
677+ | ` text.lower() ` | 转为小写 | ` "HELLO".lower() ` → ` 'hello' ` |
678+ | ` text.title() ` | 标题格式(每个单词首字母大写) | ` "hello world".title() ` → ` 'Hello World' ` |
679+ | ` text.strip() ` | 去除两端空白 | ` " hello ".strip() ` → ` 'hello' ` |
680+ | ` text.replace(old, new) ` | 字符串替换(默认全局) | ` "hi hi".replace("hi", "bye") ` → ` 'bye bye' ` |
681+ | ` text.split(sep) ` | 分割字符串为列表 | ` "a,b,c".split(",") ` → ` ['a', 'b', 'c'] ` |
682+ | ` sep.join(list) ` | 连接列表为字符串(字符串调用) | ` ",".join(['a', 'b']) ` → ` 'a,b' ` |
683+ | ` text.find(sub) ` | 查找子字符串位置(未找到返回 -1) | ` "hello".find("ll") ` → ` 2 ` |
684+ | ` text.startswith(prefix) ` | 检查开头 | ` "hello".startswith("he") ` → ` True ` |
685+ | ` text.endswith(suffix) ` | 检查结尾 | ` "hello".endswith("lo") ` → ` True ` |
686+ | ` text.isdigit() ` | 是否全为数字 | ` "123".isdigit() ` → ` True ` |
687+ | ` text.isalpha() ` | 是否全为字母 | ` "abc".isalpha() ` → ` True ` |
688+ | ` text * n ` | 重复字符串 | ` "hi" * 3 ` → ` 'hihihi' ` |
689+ | ` f"text {var}" ` | f-string 格式化(推荐) | ` f"Hello {name}" ` |
690+
668691## 练习
669692
670693处理用户输入数据,输出用户卡片:
0 commit comments