\n
\t
\\
\"
字串前的一些英文字
r"some string":r 表示不要轉義,\n 就是 \n,而不是換行,路徑上與正則最常使用f"some string":f 表示格式化字串,類似 format()
b"some string":b 表示 bytesu"some string":u 表示 unicode合併join()str.join(sequence)
sequence:表示要合併的字串列表str:表示合併時加的字元" ".join(["this", "is", "a", "test"])
# 以 (空格)合併
# this is a test
"-".join(["this", "is", "a", "test"])
# 以 - 合併
# this-is-a-test
分割splite()str.split(separator, max)
separator:表示要分割的值,預設是 (空格)max:表示分割的次數,剩下的字串會放在最後面x = "this is a test"
x.split()
# 以 (空格)分割
# ["this", "is", "a", "test"]
x = "a-b-c-d"
x.split("-", 1)
# 以 - 分割 1 次
# ["a", "b-c-d"]
轉換replace()str.replace(old, new[, max])
old 表示會被替換的字元,new 表示替換後的字元,max 為次數temp_str = "this is a test"
print(temp_str.replace("is","IS"))
print(temp_str)
# thIS IS a test
# this is a test
strip()
string.whitespace 來看那些被認為是空白字元lstrip():只移除左側的空格或指定的字元rstrip():只移除右側的空格或指定的字元x = " Hello, World "
x.strip()
# "Hello, World"
x.lstrip()
# "Hello, World "
x.rstrip()
# " Hello, World"
y = "00124abc23100"
y.strip("012")
# "4abc23"
import string
print(string.whitespace)
# 不同的系統回傳可能會不同
# 特殊字元,也可能看到一堆空白
小補充
strip,去除頭尾空白,不要想說怎麼值不一樣而找問題找半天還有一些可能有機會用到的
find()、rfind():搜尋文字回傳第一個字元的 index,rfind() 為從尾端開始count():計算字數lower():全部轉為小寫upper():全部轉為大寫startwith()、endwith():是否以指定字串開頭或結尾ljust()、rjust()、center():向左、向右或置中填滿空格或指定字元來看看長字串跟格式化文字怎麼處理吧!