11---
2- title : Python 语法速查
2+ title : Python 语法速查「考试辅助」
33date : 2024-11-24 19:42:04
44author : AI悦创
55isOriginal : true
@@ -20,6 +20,10 @@ toc: true
2020watermark : true
2121---
2222
23+ 你好,我是悦创。
24+
25+ 预祝你考试顺利!此内容为提供的速查笔记📒。
26+
2327## 1. 字符串
2428
2529### 1.1 input 获取用户输入
@@ -119,7 +123,31 @@ print(f"你好, {name}!")
119123
120124# ## 1.2 字符串的常用内置函数表
121125
122-
126+ 以下是 Python 字符串常用的内置函数的表格,包括函数名、说明和代码示例:
127+
128+ | ** 函数名** | ** 说明** | ** 代码示例** |
129+ | ------------------ | -------------------------------------------- | ------------------------------------------------------------ |
130+ | `str .lower()` | 将字符串转换为小写字母。 | `print (" Hello World!" .lower())` ➡️ `hello world!` |
131+ | `str .upper()` | 将字符串转换为大写字母。 | `print (" Hello World!" .upper())` ➡️ `HELLO WORLD !` |
132+ | `str .capitalize()` | 将字符串的首字母转换为大写,其他字母小写。 | `print (" hello world!" .capitalize())` ➡️ `Hello world!` |
133+ | `str .title()` | 将字符串的每个单词的首字母大写。 | `print (" hello world!" .title())` ➡️ `Hello World!` |
134+ | `str .strip()` | 去掉字符串首尾的空格或指定字符。 | `print (" hello " .strip())` ➡️ `hello` |
135+ | `str .lstrip()` | 去掉字符串左边的空格或指定字符。 | `print (" hello " .lstrip())` ➡️ `hello ` |
136+ | `str .rstrip()` | 去掉字符串右边的空格或指定字符。 | `print (" hello " .rstrip())` ➡️ ` hello` |
137+ | `str .replace()` | 替换字符串中的子字符串。 | `print (" hello world" .replace(" world" , " Python" ))` ➡️ `hello Python` |
138+ | `str .split()` | 按指定分隔符拆分字符串,返回列表。 | `print (" a,b,c" .split(" ," ))` ➡️ `[' a' , ' b' , ' c' ]` |
139+ | `str .join()` | 用指定字符串连接列表中的元素。 | `print (" -" .join([" a" , " b" , " c" ]))` ➡️ `a- b- c` |
140+ | `str .find()` | 返回子字符串首次出现的索引,找不到返回 - 1 。 | `print (" hello world" .find(" world" ))` ➡️ `6 ` |
141+ | `str .index()` | 返回子字符串首次出现的索引,找不到抛出异常。 | `print (" hello world" .index(" world" ))` ➡️ `6 ` |
142+ | `str .startswith()` | 检查字符串是否以指定前缀开头,返回布尔值。 | `print (" hello world" .startswith(" hello" ))` ➡️ `True ` |
143+ | `str .endswith()` | 检查字符串是否以指定后缀结尾,返回布尔值。 | `print (" hello world" .endswith(" world" ))` ➡️ `True ` |
144+ | `str .isalpha()` | 检查字符串是否全是字母,返回布尔值。 | `print (" hello" .isalpha())` ➡️ `True ` |
145+ | `str .isdigit()` | 检查字符串是否全是数字,返回布尔值。 | `print (" 123" .isdigit())` ➡️ `True ` |
146+ | `str .isalnum()` | 检查字符串是否全是字母或数字,返回布尔值。 | `print (" hello123" .isalnum())` ➡️ `True ` |
147+ | `str .isspace()` | 检查字符串是否全是空白字符,返回布尔值。 | `print (" " .isspace())` ➡️ `True ` |
148+ | `str .zfill()` | 在字符串左侧填充零,达到指定宽度。 | `print (" 42" .zfill(5 ))` ➡️ `00042 ` |
149+ | `str .format()` | 使用格式化字符串填充变量值。 | `print (" Hello, {} " .format(" World" ))` ➡️ `Hello, World` |
150+ | `str .count()` | 返回子字符串在字符串中出现的次数。 | `print (" hello world" .count(" o" ))` ➡️ `2 ` |
123151
124152
125153
0 commit comments