字符串
一句话解释
字符串 是 不可变 序列 的 characters in Python, 编写 使用 单个 or 双 quotes.
用大白话说
字符串是 序列的 characters — 文本数据. 你创建字符串使用 quotes: 'hello' or "hello" (两者都工作相同). 三 quotes '''...''' or """...""" allow multi-line strings.
Strings are immutable — you cannot change a character in place. Any operation that modifies a string (like replace or upper) returns a new string. Strings support indexing (s[0]), slicing (s[1:4]), concatenation (+), and repetition (* n).
Python strings have many useful methods: 拆分(), 连接(), strip(), replace(), upper(), lower(), 查找(), and more. f-strings (f"{值}") embed expressions directly in strings.
示例
s = "Hello, World!"
print(len(s)) # 13
print(s.upper()) # HELLO, WORLD!
print(s.split(",")) # ['Hello', ' World!']
print(s[:5]) # Hello
name = "Alice"
print(f"Hi, {name}") # Hi, Alice字符串 support len, 方法 如 upper/拆分, slicing, and f-字符串 格式化.
常见混淆
容易混淆的对象: 列表
关键区别: 字符串 是 序列 的 characters and 是 不可变. 列表 是 序列 的 任何 项 and 是 可变. 你 能 更改 列表 元素 但 不 字符串 character.
相关术语
相关资源
相关课程
- 字符串
学习 Python 字符串.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。