截取
去除空白
x = " hello world "
print(x.lstrip())
x = " hello world "
print(x.rstrip())
x = " hello world "
print(x.strip())

去除字符串中的指定字符
x = "www.baidu.com"
print(x.lstrip("bwcom."))
x = "www.baidu.com"
print(x.rstrip(".dcom"))
x = "www.baidu.com"
print(x.strip("wcom."))

去除字符串中的指定字符串
python3.9后才有的功能
x = "www.baidu.com"
print(x.removeprefix("www."))
x = "www.baidu.com"
print(x.removesuffix(".com"))
拆分
partition()
x = "www.baidu.com"
print(x.partition("."))
print(x.rpartition("."))

split()
x = "www.baidu.com"
print(x.split("."))

splitlines()
x = "www\n.baidu\n.com"
print(x.splitlines())

|