Python字符串切片未产生预期的结果

时间:2019-05-29 18:20:05

标签: python-3.x string

我的问题与字符串切片有关。我想提取以“ +”开头的字符串,直到字符串的结尾。我使用'indx'查找以'+'开头的每个子字符串的索引。除第一个子字符串外,其余所需的子字符串未正确提取。但是我的indx和字符串find()产生相同的结果,但子字符串输出错误

 text = "Hello +Shahroz You gotta laugh +TriptiAgrawal at that.user You 
 gotta laugh at that +Thexyz1213 dudes been so broke"
 indx=0
for word in text.split():
    if word.startswith('+'):
        print("Yes True")
        print("indx1")

        txt=text[indx:]
        print("txt:"+txt)
  indx += len(word)+1

输出应为:

 +Shahroz You gotta laugh +TriptiAgrawal at that.user You 
 gotta laugh at that +Thexyz1213 dudes been so broke

 +TriptiAgrawal at that.user You 
 gotta laugh at that +Thexyz1213 dudes been so broke

 +Thexyz1213 dudes been so broke

0 个答案:

没有答案