Python是否从字符串的开头删除到第一个特定字符?
假设我有以下字符串:Python是否从字符串的开头删除到第一个特定字符?,python,string,split,Python,String,Split,假设我有以下字符串: this is ;a cool test 如何从启动到第一次a删除所有内容发生了什么? 预期的输出将是一个冷静的测试 我只知道如何使用括号符号删除固定数量的字符,这在这里没有帮助,因为的位置不是固定的。使用str.find和切片 Ex: s = "this is ;a cool test; Hello World." print(s[s.find(";")+1:]) # --> a cool test; Hello World. s = "this is ;a c
this is ;a cool test
如何从启动到第一次a删除所有内容代码>发生了什么?
预期的输出将是一个冷静的测试
我只知道如何使用括号符号删除固定数量的字符,这在这里没有帮助,因为的位置代码>不是固定的。使用str.find
和切片
Ex:
s = "this is ;a cool test; Hello World."
print(s[s.find(";")+1:])
# --> a cool test; Hello World.
s = "this is ;a cool test; Hello World."
print(s.split(";", 1)[-1])
# --> a cool test; Hello World.
或者使用str.split
Ex:
s = "this is ;a cool test; Hello World."
print(s[s.find(";")+1:])
# --> a cool test; Hello World.
s = "this is ;a cool test; Hello World."
print(s.split(";", 1)[-1])
# --> a cool test; Hello World.
你可以使用正则表达式
import re
x = "this is ;a cool test"
x = re.sub(r'^[^;]+;','',x)
print(x)