一、字符串的基本操作

1.1 创建字符串

# 三种创建方式
str1 = '单引号字符串'
str2 = "双引号字符串"
str3 = '''三引号字符串,
可以跨多行'''

1.2 字符串索引和切片

s = "Hello, Python!"

# 索引
print(s[0])      # H (第一个字符)
print(s[-1])     # ! (最后一个字符)
print(s[7])      # P

# 切片
print(s[0:5])    # Hello (包含起点,不包含终点)
print(s[7:])     # Python! (从索引7到末尾)
print(s[:5])     # Hello (从头到索引4)
print(s[-7:-1])  # Python (负索引切片)
print(s[::2])    # HloPto! (步长为2)

二、常用的字符串方法

2.1 大小写转换

s = "hello World"

print(s.upper())        # HELLO WORLD
print(s.lower())        # hello world
print(s.capitalize())   # Hello world
print(s.title())        # Hello World
print(s.swapcase())     # HELLO wORLD

2.2 查找和替换

s = "Hello, Python! Python is great."

# 查找
print(s.find("Python"))      # 7 (返回第一次出现的索引)
print(s.rfind("Python"))     # 15 (从右边开始查找)
print(s.index("Python"))     # 7 (类似find,但找不到会报错)
print(s.rindex("Python"))    # 15

# 替换
print(s.replace("Python", "Java"))  # Hello, Java! Java is great.
print(s.replace("Python", "Java", 1))  # 只替换一次

# 检查开头和结尾
print(s.startswith("Hello"))  # True
print(s.endswith("great."))   # True

2.3 分割和连接

s = "apple,banana,orange"

# 分割
print(s.split(","))           # ['apple', 'banana', 'orange']
print(s.split(",", 1))        # ['apple', 'banana,orange'] (最多分割1次)

s2 = "line1\nline2\nline3"
print(s2.splitlines())        # ['line1', 'line2', 'line3']

# 分区
print(s.partition(","))       # ('apple', ',', 'banana,orange')
print(s.rpartition(","))      # ('apple,banana', ',', 'orange')

# 连接
list_fruits = ['apple', 'banana', 'orange']
print(",".join(list_fruits))  # apple,banana,orange

2.4 去除空白字符

s = "  hello world  \t\n"

print(s.strip())      # "hello world" (去除两边空白)
print(s.lstrip())     # "hello world  \t\n" (去除左边空白)
print(s.rstrip())     # "  hello world" (去除右边空白)

# 去除指定字符
s2 = "***hello***"
print(s2.strip("*"))  # "hello"

2.5 判断方法

s1 = "Hello123"
s2 = "12345"
s3 = "HELLO"
s4 = "hello"
s5 = "Hello World"
s6 = "   "

print(s1.isalnum())    # True (字母或数字)
print(s1.isalpha())    # False (纯字母)
print(s2.isdigit())    # True (纯数字)
print(s3.isupper())    # True (全大写)
print(s4.islower())    # True (全小写)
print(s5.istitle())    # True (每个单词首字母大写)
print(s6.isspace())    # True (全是空白字符)
print(s1.isascii())    # True (ASCII字符)

2.6 对齐和填充

s = "hello"

print(s.center(11, "*"))   # ***hello***
print(s.ljust(10, "-"))    # hello-----
print(s.rjust(10, "-"))    # -----hello
print(s.zfill(10))         # 00000hello

2.7 计数和长度

s = "hello world, hello python"

print(len(s))               # 25 (字符串长度)
print(s.count("hello"))     # 2 (子字符串出现次数)
print(s.count("l"))         # 4 (字符出现次数)
print(s.count("o", 0, 10))  # 1 (在指定范围内计数)

三、字符串格式化

3.1 传统格式化(%)

name = "Alice"
age = 25

print("Name: %s, Age: %d" % (name, age))
print("Price: %.2f" % 99.987)  # 99.99
print("Percent: %d%%" % 80)    # 80%

3.2 format()方法

# 位置参数
print("{} is {} years old".format("Alice", 25))

# 关键字参数
print("{name} is {age} years old".format(name="Bob", age=30))

# 索引
print("{1} comes before {0}".format("B", "A"))

# 格式化数字
print("Price: {:.2f}".format(99.987))  # 99.99
print("Hex: {:x}".format(255))         # ff
print("Percent: {:.1%}".format(0.8))   # 80.0%

3.3 f-string(Python 3.6+)

name = "Alice"
age = 25
height = 1.65

print(f"{name} is {age} years old")
print(f"Price: {99.987:.2f}")  # 99.99
print(f"Hex: {255:x}")         # ff
print(f"Expression: {age * 2}") # 50
print(f"Height: {height:.2f}m") # 1.65m

四、字符串编码和转换

4.1 编码转换

s = "你好,世界"

# 编码为字节
b = s.encode("utf-8")  # b'\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x8c\xe4\xb8\x96\xe7\x95\x8c'
b2 = s.encode("gbk")

# 解码为字符串
s2 = b.decode("utf-8")
print(s2)  # 你好,世界

4.2 maketrans和translate

# 创建转换表
trans_table = str.maketrans("aeiou", "12345")

s = "hello world"
print(s.translate(trans_table))  # h2ll4 w4rld

# 删除指定字符
trans_table2 = str.maketrans("", "", "aeiou")
print(s.translate(trans_table2))  # hll wrld

五、其他有用的方法

5.1 expandtabs()

s = "Name\tAge\tCity"
print(s.expandtabs(20))
# Name                Age                 City

5.2 partition() 和 rpartition()

url = "www.example.com/page"

print(url.partition("."))    # ('www', '.', 'example.com/page')
print(url.rpartition("."))   # ('www.example', '.', 'com/page')

5.3 检查内容

s1 = "Hello123"
s2 = "123"
s3 = "Hello"

print(s1.isalnum())  # True (字母或数字)
print(s2.isdecimal())  # True (十进制数字)
print(s3.isalpha())  # True (纯字母)
print(s2.isnumeric())  # True (数值字符)

六、字符串性能优化技巧

6.1 使用join代替+

# 不推荐 - 创建多个临时字符串
result = ""
for i in range(10000):
    result += str(i)

# 推荐 - 更高效
parts = []
for i in range(10000):
    parts.append(str(i))
result = "".join(parts)

6.2 使用in进行成员检查

s = "hello world"

# 检查子字符串
if "world" in s:
    print("Found!")

# 检查字符
if "h" in s:
    print("Contains 'h'")

七、实用示例

7.1 数据清洗

def clean_string(text):
    """清洗字符串"""
    # 去除空白字符
    text = text.strip()
    # 转换为小写
    text = text.lower()
    # 替换多个空格为单个空格
    import re
    text = re.sub(r'\s+', ' ', text)
    return text

sample = "  Hello   World  \n"
print(clean_string(sample))  # "hello world"

7.2 字符串验证

def validate_input(input_str):
    """验证用户输入"""
    if not input_str:
        return "输入不能为空"
    
    if len(input_str) < 3:
        return "输入太短"
    
    if not input_str.isalnum():
        return "只能包含字母和数字"
    
    return "输入有效"

print(validate_input("abc123"))  # 输入有效
print(validate_input("ab"))      # 输入太短

7.3 模板生成

def generate_email_template(name, product):
    """生成邮件模板"""
    template = """
尊敬的{name}:

感谢您购买{product}!
如有任何问题,请随时联系我们。

祝好!
客服团队
"""
    return template.format(name=name, product=product)

print(generate_email_template("张三", "Python编程书籍"))

总结

方法类型

常用方法

说明

大小写转换

upper(), lower(), title()

改变字符串大小写

查找替换

find(), replace(), count()

查找和替换内容

分割连接

split(), join(), partition()

字符串分割和连接

去除空白

strip(), lstrip(), rstrip()

去除空白字符

判断检查

isalpha(), isdigit(), startswith()

检查字符串属性

格式化

format(), f-string

字符串格式化

编码转换

encode(), decode()

编码转换

记住:字符串是不可变对象,所有字符串方法都不会修改原字符串,而是返回一个新的字符串。