本文目录导读:

我来详细讲解Python中截取指定字符串的多种方法:
字符串切片(最常用)
text = "Hello, Python World!" # 基本语法:string[start:stop:step] print(text[0:5]) # "Hello" - 索引0到4 print(text[7:13]) # "Python" - 索引7到12 print(text[:5]) # "Hello" - 从开头到索引4 print(text[7:]) # "Python World!" - 从索引7到最后 print(text[::-1]) # "!dlroW nohtyP ,olleH" - 反转字符串
查找特定位置后截取
# 方法1:find() + 切片
email = "user@example.com"
at_pos = email.find("@")
username = email[:at_pos] # "user"
domain = email[at_pos+1:] # "example.com"
# 方法2:index() + 切片
phone = "+86-138-1234-5678"
dash1 = phone.index("-")
area_code = phone[:dash1] # "+86"
使用split()分割字符串
# 基础分割
url = "https://www.example.com/path/to/page"
parts = url.split("/")
print(parts) # ['https:', '', 'www.example.com', 'path', 'to', 'page']
# 提取域名
domain = parts[2] # "www.example.com"
# 限制分割次数
csv_data = "apple,banana,orange,grape"
fruits = csv_data.split(",", 2) # ['apple', 'banana', 'orange,grape']
正则表达式提取
import re
# 提取邮箱
text = "联系邮箱: user@example.com, 备用邮箱: admin@test.org"
emails = re.findall(r'[\w\.-]+@[\w\.-]+\.\w+', text)
print(emails) # ['user@example.com', 'admin@test.org']
# 提取电话号码
phone_text = "电话: 138-1234-5678 或 010-8888-6666"
phones = re.findall(r'\d{3}-\d{4}-\d{4}', phone_text)
print(phones) # ['138-1234-5678', '010-8888-6666']
# 提取特定格式的日期
dates = "日期: 2024-01-15, 2024-02-20"
found_dates = re.findall(r'\d{4}-\d{2}-\d{2}', dates)
print(found_dates) # ['2024-01-15', '2024-02-20']
实际应用案例
# 案例1:从HTML中提取标题
html = "<title>Python学习教程</title>"
start = html.find("<title>") + len("<title>")
end = html.find("</title>")= html[start:end]
print(title) # "Python学习教程"
# 案例2:提取文件扩展名
filename = "document.pdf"
if "." in filename:
name, ext = filename.rsplit(".", 1)
print(f"文件名: {name}") # "document"
print(f"扩展名: {ext}") # "pdf"
# 案例3:提取配置信息
config = "host=localhost;port=3306;db=test"
items = config.split(";")
for item in items:
key, value = item.split("=")
print(f"{key}: {value}")
# host: localhost
# port: 3306
# db: test
封装成函数
def extract_between(text, start_str, end_str):
"""提取两个字符串之间的内容"""
start = text.find(start_str)
if start == -1:
return None
start += len(start_str)
end = text.find(end_str, start)
if end == -1:
return text[start:]
return text[start:end]
# 使用示例
text = "【重要】这是通知内容【结束】"
result = extract_between(text, "【重要】", "【结束】")
print(result) # "这是通知内容"
# 更强大的版本,支持正则
def extract_pattern(text, pattern):
"""使用正则表达式提取所有匹配"""
import re
return re.findall(pattern, text)
# 提取所有数字
data = "价格: $99.99, 数量: 5, 折扣: 10%"
numbers = extract_pattern(data, r'\d+\.?\d*')
print(numbers) # ['99.99', '5', '10']
性能优化建议
# 对于大量文本处理,使用find或index比正则更快
def extract_quick(text, prefix, suffix):
"""快速提取(不使用正则)"""
start = text.find(prefix)
if start == -1:
return ""
start += len(prefix)
end = text.find(suffix, start)
if end == -1:
return text[start:]
return text[start:end]
# 对于复杂模式,使用正则表达式
import re
def extract_complex(text, pattern):
"""复杂模式提取(使用正则)"""
match = re.search(pattern, text)
return match.group(1) if match else ""
选择建议
- 简单固定位置:使用切片
text[0:5] - 动态位置:使用
find()+ 切片 - 按分隔符提取:使用
split() - 复杂模式匹配:使用正则表达式
re.findall()
根据你的具体需求选择最合适的方法!
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播