python - 字符串常用操作

106 阅读3分钟

字符串(str)是 Python 中最常用的数据类型之一,用于存储文本数据。Python 提供了丰富的字符串操作方法,包括拼接、切片、查找、替换、格式化等。以下是字符串的常用操作总结:


1. 字符串创建

  • 直接定义:用单引号 '、双引号 " 或三引号 '''/""" 定义。

    s1 = 'Hello'
    s2 = "World"
    s3 = '''多行
    字符串'''
    s4 = """Python
    字符串"""
    
  • 转义字符

    path = "C:\Users\Name\file.txt"  # 使用 `` 转义
    path2 = r"C:\Users\Name\file.txt"   # 原始字符串(`r` 前缀,忽略转义)
    

2. 字符串拼接

  • + 拼接

    s1 = "Hello"
    s2 = "World"
    s3 = s1 + " " + s2  # 输出: "Hello World"
    
  • join() 方法(高效拼接多个字符串):

    words = ["Python", "is", "awesome"]
    sentence = " ".join(words)  # 输出: "Python is awesome"
    
  • * 重复字符串

    s = "Hi" * 3  # 输出: "HiHiHi"
    

3. 字符串索引与切片

  • 索引访问(从 0 开始):

    s = "Python"
    print(s[0])   # 输出: 'P'
    print(s[-1])  # 输出: 'n'(负索引从 -1 开始)
    
  • 切片操作[start:stop:step]):

    s = "Hello, World!"
    print(s[0:5])     # 输出: "Hello"(不包含 stop)
    print(s[7:12])    # 输出: "World"
    print(s[::2])     # 输出: "Hlo ol"(步长为 2)
    print(s[::-1])    # 输出: "!dlroW ,olleH"(反转字符串)
    

4. 字符串常用方法

(1) 大小写转换

s = "python"
print(s.upper())   # 输出: "PYTHON"
print(s.lower())   # 输出: "python"
print(s.capitalize())  # 输出: "Python"(首字母大写)
print(s.title())    # 输出: "Python"(每个单词首字母大写)

(2) 去除空格

s = "   Hello   "
print(s.strip())   # 输出: "Hello"(去除首尾空格)
print(s.lstrip())  # 输出: "Hello   "(去除左侧空格)
print(s.rstrip())  # 输出: "   Hello"(去除右侧空格)

(3) 查找与替换

s = "hello world"
print(s.find("world"))  # 输出: 6(返回子串索引,不存在返回 -1)
print(s.index("world")) # 输出: 6(同 `find()`,但不存在时报错)
print(s.replace("world", "Python"))  # 输出: "hello Python"
print(s.count("l"))     # 输出: 3(统计子串出现次数)

(4) 分割与连接

python
s = "apple,banana,orange"
fruits = s.split(",")  # 输出: ["apple", "banana", "orange"]
new_s = "-".join(fruits)  # 输出: "apple-banana-orange"

(5) 判断字符串内容

s = "123"
print(s.isdigit())   # 输出: True(是否全为数字)
print(s.isalpha())   # 输出: False(是否全为字母)
print(s.isalnum())   # 输出: True(是否全为字母或数字)
print(s.isspace())   # 输出: False(是否全为空格)

(6) 其他实用方法

python
s = "hello"
print(s.startswith("he"))  # 输出: True(是否以指定字符串开头)
print(s.endswith("lo"))    # 输出: True(是否以指定字符串结尾)
print(len(s))              # 输出: 5(字符串长度)
print("l" in s)            # 输出: True(判断子串是否存在)

5. 字符串格式化

(1) f-string(Python 3.6+ 推荐)

name = "Alice"
age = 25
print(f"My name is {name}, and I'm {age} years old.")
# 输出: "My name is Alice, and I'm 25 years old."

(2) format() 方法

s = "My name is {}, and I'm {} years old.".format("Bob", 30)
print(s)  # 输出: "My name is Bob, and I'm 30 years old."

(3) % 格式化(旧式,不推荐)

s = "My name is %s, and I'm %d years old." % ("Charlie", 35)
print(s)  # 输出: "My name is Charlie, and I'm 35 years old."

6. 字符串编码与解码

  • 编码(字符串 → 字节)

    s = "你好"
    bytes_data = s.encode("utf-8")  # 输出: b'\xe4\xbd\xa0\xe5\xa5\xbd'
    
  • 解码(字节 → 字符串)

    bytes_data = b'\xe4\xbd\xa0\xe5\xa5\xbd'
    s = bytes_data.decode("utf-8")  # 输出: "你好"
    

7. 字符串不可变性

  • 字符串一旦创建,不能修改(类似元组):

    s = "hello"
    s[0] = "H"  # 报错: TypeError: 'str' object does not support item assignment
    
  • 修改字符串需创建新字符串:

    s = "hello"
    s = "H" + s[1:]  # 输出: "Hello"
    

8. 常见应用场景

  1. 用户输入处理

    user_input = input("请输入名字: ")
    print(f"你好, {user_input}!")
    
  2. 文件路径拼接

    import os
    path = os.path.join("folder", "file.txt")  # 跨平台路径拼接
    
  3. 数据清洗

    text = "  Python is Awesome!  "
    cleaned_text = text.strip().lower().replace("!", "")
    print(cleaned_text)  # 输出: "python is awesome"
    
  4. 正则表达式匹配(需导入 re 模块):

    import re
    text = "我的电话是 123-456-7890"
    phone = re.search(r"\d{3}-\d{3}-\d{4}", text).group()
    print(phone)  # 输出: "123-456-7890"
    

总结

操作类型常用方法
创建' '" "''' '''""" """r" "
拼接+join()*
切片[start:stop:step]
查找find()index()count()in
替换replace()
分割split()rsplit()
大小写upper()lower()capitalize()title()
格式化f-string, format()%
判断isdigit()isalpha()startswith()endswith()