简体   繁体   English

Python:如何使用字符串对字符串进行切片?

[英]Python: How to slice string using string?

Assuming that the user entered:假设用户输入:

"i like eating big apples"

Want to remove "eating" and "apples" together with whatever is in between these two words.想要删除“吃”和“苹果”以及这两个词之间的任何内容。 Output in this case Output 在这种情况下

"i like"

In another case, if the user entered:在另一种情况下,如果用户输入:

"i like eating apples very much"

Expected output:预期 output:

"i like very much"

And I want to slice the input starting from "eating" to "apples" (However, the index cannot be used as you are unsure how long the user is going to type, but it is guaranteed that "eating" and "apples" will be entered)我想从“吃”到“苹果”分割输入(但是,不能使用索引,因为您不确定用户要输入多长时间,但可以保证“吃”和“苹果”会被输入)

So, is there any way that we can slide without using the index, instead, we indicate the start and end of the slide with another string?那么,有没有什么方法可以不使用索引滑动,而是用另一个字符串来指示幻灯片的开始和结束?

You can do the follwoing:您可以执行以下操作:

s = "i like eating big apples"
start_ = s.find("eating")
end_ = s.find("apples") + len("apples")
s[start_:end_]  # 'eating big apples'

Using find() to find the starting indices of the desired word in the string, and then adjust the start_ / end_ to your needs.使用find()在字符串中查找所需单词的起始索引,然后根据需要调整start_ / end_

To remove the sub string:要删除子字符串:

s[:start_] + s[end_:]  # i like

And for:对于:

s = "i like eating apples very much"
end_ = s.find("apples") + len("apples")
start_ = s.find("eating")
s[:start_] + s[end_:]  # 'i like  very much'

Slicing a string in python is like this:在 python 中切片一个字符串是这样的:

mystr = "i like eating big apples"
print(mystr[10:20])

This means between the 10th boundary of characters in the string and the 20th.这意味着在字符串的第 10 个字符边界和第 20 个字符之间。 So it will become: ing big ap .所以它会变成: ing big ap

Now the question is how to find out which index 'eating' starts and 'apple' ends.现在的问题是如何找出“吃”开始和“苹果”结束的索引。

Use the .index method to find the beginning of something in a string.使用.index方法在字符串中查找某些内容的开头。

mystr.index('eating') returns 7, so if you print mystr[7:] (which means from the 7th index till the last of the string) you'll have 'eating big apples' . mystr.index('eating')返回 7,因此如果您打印mystr[7:] (这意味着从第 7 个索引到字符串的最后一个),您将获得'eating big apples'

The second part is a little tricky.第二部分有点棘手。 If you use mystr.index('apple') , you'll get the beginning of apple, (18), so mystr[7:18] will give you 'eating big ' .如果你使用mystr.index('apple') ,你会得到苹果的开头,(18),所以 mystr[7:18] 会给你'eating big '

In fact you should go some characters further to include the apple word too, which is 5 chars exactly, and this number will be returned by len('apple') .实际上,您应该 go 一些字符进一步包括苹果词,这正好是 5 个字符,这个数字将由len('apple')返回。 So the final result is:所以最终的结果是:

start = mystr.index('eating')
stop = mystr.index('apple') + len('apple')
print(mystr[start:stop])

maybe you can use this:也许你可以使用这个:

txt = "Hello, welcome to my world."

x = txt.find("welcome")

print(x)

Which outputs: 7 To find "eating" and "apple"输出:7 找到“吃”和“苹果”

S = "i like eating big apples"
Index = S.find("eating")
output = S[Index:-1]

Use find() or rfind() method for searching substring's occurrence indices, then paste method's result into slice:使用find()rfind()方法搜索子字符串的出现索引,然后将方法的结果粘贴到切片中:

s = "i like eating big apples"
substr = s[s.rfind("eating"):s.rfind("apples")]

You can use str.partition to split string into three parts.您可以使用str.partition将字符串拆分为三个部分。

In [112]: s = "i like eating apples very much"                                                                                                                            
                                                                                                                                                                          
In [113]: h, _, t = s.partition('eating')                                                                                                                                 
                                                                                                                                                                          
In [114]: _, _, t = t.partition('apples')                                                                                                                                 
                                                                                                                                                                          
In [115]: h + t                                                                                                                                                           
Out[115]: 'i like  very much'                                                                                                                                             
                                                                                                                                                                          
In [116]: s = "i like eating big apples"                                                                                                                                  
                                                                                                                                                                          
In [117]: h, _, t = s.partition('eating')                                                                                                                                 
                                                                                                                                                                          
In [118]: _, _, t = t.partition('apples')                                                                                                                                 
                                                                                                                                                                          
In [119]: h + t                                                                                                                                                           
Out[119]: 'i like '                   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM