Python打印拆分以char 5-10为例
我想把我的绳子分开 假设字符串是Python打印拆分以char 5-10为例,python,printing,split,Python,Printing,Split,我想把我的绳子分开 假设字符串是你好。 我只想打印how are(意思是在hello之后开始,在are之后结束 我现在的代码只是在hello之后开始,但是打印所有剩余的代码。 想要避开你 ReadJSONFile=JSONResponseFile.read() # this is the txt file with the line print ReadJSONFile.split('hellow',1)[1] # this gives me everything after hello 组
你好
。
我只想打印how are(意思是在hello之后开始,在are之后结束
我现在的代码只是在hello之后开始,但是打印所有剩余的代码。
想要避开你
ReadJSONFile=JSONResponseFile.read() # this is the txt file with the line
print ReadJSONFile.split('hellow',1)[1] # this gives me everything after hello
组合两个
str.split
调用:
>>> s = 'hello how are you'
>>> s.split('hello', 1)[-1]
' how are you'
>>> s.split('hello', 1)[-1].split('you', 1)[0]
' how are '
>>> s.split('hello', 1)[-1].split('you', 1)[0].strip() # remove surrounding spaces
'how are'
您可以使用字符串切片:
>>> s = "hello how are you"
>>> s[6:13]
'how are'
如果有起始索引和结束索引,则可以使用切片表示法提取字符串的起始索引:
str='你好吗'
#你想要从索引6(h)到12(e)
打印str[6:12+1]
这应该有帮助:(使用索引和切片)
…这在Python教程的语言介绍的第一页上有详细的解释:感谢大家的快速响应,
>>> start = h.index('hello')+len('hello')
>>> end =h.index('you')
>>> h[start:end].strip()
'how are'