Python split() 字串分割
split() 方法用來將字串依據指定的分隔符號分割成 list。
基本用法
text = "apple,banana,orange"
result = text.split(",")
print(result) # ['apple', 'banana', 'orange']
語法
str.split(sep=None, maxsplit=-1)
sep:分隔符號,預設為 None(空白字元)maxsplit:最大分割次數,-1 表示無限制
使用空白分割(預設)
當不指定分隔符號時,會以任意空白字元分割,並自動忽略多餘的空白:
text = "Hello World Python"
result = text.split()
print(result) # ['Hello', 'World', 'Python']
# 包含換行和 Tab
text = "Hello\nWorld\tPython"
result = text.split()
print(result) # ['Hello', 'World', 'Python']
指定分隔符號
# 逗號分隔
text = "a,b,c,d"
print(text.split(",")) # ['a', 'b', 'c', 'd']
# 冒號分隔
text = "name:Alice:age:25"
print(text.split(":")) # ['name', 'Alice', 'age', '25']
# 多字元分隔符號
text = "apple--banana--orange"
print(text.split("--")) # ['apple', 'banana', 'orange']
maxsplit 參數
限制分割次數:
text = "a,b,c,d,e"
print(text.split(",", 1)) # ['a', 'b,c,d,e']
print(text.split(",", 2)) # ['a', 'b', 'c,d,e']
print(text.split(",", 3)) # ['a', 'b', 'c', 'd,e']
處理空字串
# 連續分隔符號會產生空字串
text = "a,,b,,c"
print(text.split(",")) # ['a', '', 'b', '', 'c']
# 使用 filter 過濾空字串
result = list(filter(None, text.split(",")))
print(result) # ['a', 'b', 'c']
# 或使用 list comprehension
result = [x for x in text.split(",") if x]
print(result) # ['a', 'b', 'c']
splitlines() - 按行分割
text = """Line 1
Line 2
Line 3"""
lines = text.splitlines()
print(lines) # ['Line 1', 'Line 2', 'Line 3']
# 保留換行符號
lines = text.splitlines(keepends=True)
print(lines) # ['Line 1\n', 'Line 2\n', 'Line 3']
rsplit() - 從右邊開始分割
text = "a,b,c,d,e"
print(text.rsplit(",", 1)) # ['a,b,c,d', 'e']
print(text.rsplit(",", 2)) # ['a,b,c', 'd', 'e']
實際範例
解析 CSV 資料
csv_line = "Alice,25,Taipei,Engineer"
fields = csv_line.split(",")
name, age, city, job = fields
print(f"{name} is a {age}-year-old {job} from {city}")
解析 URL 路徑
url = "https://example.com/users/123/profile"
parts = url.split("/")
print(parts) # ['https:', '', 'example.com', 'users', '123', 'profile']
# 取得 user_id
user_id = url.split("/")[-2]
print(user_id) # 123
解析 key=value 格式
config = "host=localhost;port=5432;database=mydb"
settings = {}
for item in config.split(";"):
key, value = item.split("=")
settings[key] = value
print(settings) # {'host': 'localhost', 'port': '5432', 'database': 'mydb'}
處理使用者輸入
# 一次讀取多個數字
user_input = "10 20 30 40 50"
numbers = [int(x) for x in user_input.split()]
print(numbers) # [10, 20, 30, 40, 50]
print(sum(numbers)) # 150
取得檔案名稱和副檔名
filename = "document.backup.txt"
# 使用 rsplit 限制分割一次
name, ext = filename.rsplit(".", 1)
print(f"Name: {name}, Extension: {ext}")
# Name: document.backup, Extension: txt
搭配 join() 使用
split() 和 join() 經常搭配使用:
text = "hello world python"
# 分割後重新組合
words = text.split()
result = "-".join(words)
print(result) # hello-world-python
# 移除多餘空白
text = " hello world python "
result = " ".join(text.split())
print(result) # hello world python
注意事項
# split() 和 split(" ") 的差異
text = "hello world"
print(text.split()) # ['hello', 'world'](忽略多餘空白)
print(text.split(" ")) # ['hello', '', 'world'](保留空字串)