1

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?

4
  • What is your expected output ? Commented Nov 30, 2020 at 9:07
  • Expected to remove "eating big apples" and output "i like", but if the user entered "i like eating apples very much", the two words "eating apples" and whatever in between should be removed, so output "i like very much" P.S. Sorry if my explanation is hard to understand... Commented Nov 30, 2020 at 9:11
  • 1
    No, slicing in strings works with integer indices. There are other approaches, but you can't use slicing with strings. Just find the positions you want. Commented Nov 30, 2020 at 9:14
  • Oh... Possible to show some of the other ways to get the expected output? Commented Nov 30, 2020 at 9:15

6 Answers 6

1

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])
Sign up to request clarification or add additional context in comments.

Comments

1

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'

Comments

0

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"

Comments

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

2 Comments

The OP's expected output for that input should be "i like".
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
0

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")]

Comments

0

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 '                   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.