1 定义字符串
text = "Hello, World!"
2 多行字符串
multi_line_text = """This is
a multi-line
string."""
3 字符串拼接
greeting = "Hello"
name = "Alice"
message = greeting + ", " + name + "!"
4 格式化字符串
formatted_message = f"{greeting}, {name}!"
5 字符串长度
length = len(text)
6 访问字符串中的字符
first_char = text[0]
7 切片
substring = text[0:5] # 输出: Hello
8 转换大小写
upper_case = text.upper()
lower_case = text.lower()
9 去除空白字符
trimmed_text = " Hello ".strip()
10 查找子字符串
position = text.find("World") # 输出: 7
11 替换子字符串
replaced_text = text.replace("World", "Python")
12 检查字符串开始或结束
starts_with_hello = text.startswith("Hello")
ends_with_exclamation = text.endswith("!")
13 分割字符串
words = text.split(", ") # 输出: ['Hello', 'World!']
14 合并列表为字符串
joined_text = ", ".join(words) # 输出: "Hello, World!"