跳转到内容
主菜单
主菜单
移至侧栏
隐藏
导航
首页
最近更改
随机页面
MediaWiki帮助
代码酷
搜索
搜索
中文(中国大陆)
外观
创建账号
登录
个人工具
创建账号
登录
未登录编辑者的页面
了解详情
贡献
讨论
编辑“︁
Python 字符串方法
”︁(章节)
页面
讨论
大陆简体
阅读
编辑
编辑源代码
查看历史
工具
工具
移至侧栏
隐藏
操作
阅读
编辑
编辑源代码
查看历史
常规
链入页面
相关更改
特殊页面
页面信息
外观
移至侧栏
隐藏
您的更改会在有权核准的用户核准后向读者展示。
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
== 常用字符串方法详解 == === 大小写转换 === Python提供了多个方法用于改变字符串的大小写: * '''<code>str.upper()</code>''' - 将字符串中的所有字符转换为大写 * '''<code>str.lower()</code>''' - 将字符串中的所有字符转换为小写 * '''<code>str.capitalize()</code>''' - 将字符串的首字母大写,其余字母小写 * '''<code>str.title()</code>''' - 将字符串中每个单词的首字母大写 <syntaxhighlight lang="python"> text = "python programming is fun" print(text.upper()) # 输出: PYTHON PROGRAMMING IS FUN print(text.lower()) # 输出: python programming is fun print(text.capitalize()) # 输出: Python programming is fun print(text.title()) # 输出: Python Programming Is Fun </syntaxhighlight> === 查找和替换 === 这些方法用于在字符串中查找子串或进行替换操作: * '''<code>str.find(sub[, start[, end]])</code>''' - 返回子串sub在字符串中第一次出现的索引,如果未找到则返回-1 * '''<code>str.index(sub[, start[, end]])</code>''' - 类似于find(),但如果未找到会引发ValueError * '''<code>str.replace(old, new[, count])</code>''' - 返回字符串的副本,其中所有出现的old子串都被替换为new子串 <syntaxhighlight lang="python"> text = "Hello, world! Welcome to Python world." print(text.find("world")) # 输出: 7 print(text.find("Python")) # 输出: 23 print(text.find("Java")) # 输出: -1 print(text.replace("world", "universe")) # 输出: Hello, universe! Welcome to Python universe. print(text.replace("world", "universe", 1)) # 输出: Hello, universe! Welcome to Python world. </syntaxhighlight> === 分割和连接 === 这些方法用于分割字符串或将多个字符串连接起来: * '''<code>str.split([sep[, maxsplit]])</code>''' - 使用sep作为分隔符分割字符串,返回列表 * '''<code>str.join(iterable)</code>''' - 使用字符串作为分隔符,将可迭代对象中的字符串元素连接起来 <syntaxhighlight lang="python"> text = "apple,banana,orange,grape" fruits = text.split(",") print(fruits) # 输出: ['apple', 'banana', 'orange', 'grape'] words = ["Python", "is", "awesome"] sentence = " ".join(words) print(sentence) # 输出: Python is awesome </syntaxhighlight> === 去除空白字符 === 这些方法用于去除字符串开头或结尾的空白字符: * '''<code>str.strip([chars])</code>''' - 返回去除开头和结尾指定字符的字符串副本 * '''<code>str.lstrip([chars])</code>''' - 仅去除开头的指定字符 * '''<code>str.rstrip([chars])</code>''' - 仅去除结尾的指定字符 <syntaxhighlight lang="python"> text = " Hello, Python! " print(text.strip()) # 输出: "Hello, Python!" print(text.lstrip()) # 输出: "Hello, Python! " print(text.rstrip()) # 输出: " Hello, Python!" </syntaxhighlight> === 判断字符串属性 === 这些方法用于检查字符串是否具有某些特性: * '''<code>str.startswith(prefix[, start[, end]])</code>''' - 检查字符串是否以prefix开头 * '''<code>str.endswith(suffix[, start[, end]])</code>''' - 检查字符串是否以suffix结尾 * '''<code>str.isalpha()</code>''' - 检查字符串是否只包含字母字符 * '''<code>str.isdigit()</code>''' - 检查字符串是否只包含数字字符 <syntaxhighlight lang="python"> text = "Python3" print(text.startswith("Py")) # 输出: True print(text.endswith("3")) # 输出: True print("abc".isalpha()) # 输出: True print("123".isdigit()) # 输出: True print("abc123".isalpha()) # 输出: False </syntaxhighlight> === 格式化字符串 === Python提供了多种字符串格式化方法: * '''<code>str.format()</code>''' - 使用位置参数或关键字参数进行格式化 * '''f-strings''' (Python 3.6+) - 在字符串前加f前缀,可以直接在字符串中嵌入表达式 <syntaxhighlight lang="python"> # 使用format方法 name = "Alice" age = 25 print("My name is {} and I'm {} years old.".format(name, age)) # 输出: My name is Alice and I'm 25 years old. # 使用f-strings print(f"My name is {name} and I'm {age} years old.") # 输出: My name is Alice and I'm 25 years old. </syntaxhighlight>
摘要:
请注意,所有对代码酷的贡献均被视为依照知识共享署名-非商业性使用-相同方式共享发表(详情请见
代码酷:著作权
)。如果您不希望您的文字作品被随意编辑和分发传播,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)