简体   繁体   中英

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

"i like"

In another case, if the user entered:

"i like eating apples very much"

Expected 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.

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:

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

This means between the 10th boundary of characters in the string and the 20th. So it will become: 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.

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' .

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 ' .

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') . 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"

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:

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.

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 '                   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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