简体   繁体   中英

How can I slice a string from the index of the first occurrence of a sub string to the second occurrence of a sub string in Python?

I'm working on a homework assignment where I have to figure out how to slice a string starting at the index of the first occurrence of a sub string to the index of the second occurrence of that sub string. The problem is specifically trying to slice the string "juxtaposition" from the first "t" to the second "t" and we are supposed to use .find() to do so, but I haven't been able to figure it out.

I've tried to use WHILE to create a loop to find the index of the different occurrences of the sub string and then use that to slice the string, but I haven't been able to make it work.

This is what I've been able to come up with so far:

long_word = "juxtaposition"
location = long_word.find("t")
start_index = location
stop_index = 0
while location != -1:
    stop_index + long_word.find("t",location + 1)
print(long_word[int(start_index),int(stop_index)])

When I ran this it didn't show an error message but it doesn't show an output either, and in order to edit the cell again I have to interrupt the kernel.

I believe this will work to find the string of the first and second occurrence using only the .find() method, the printed string includes the first and second occurrence:

long_word = "juxtaposition"
location = long_word.find("t")
print(long_word[location+1:])
second_location = long_word[location+1:].find("t")
print(second_location)
print(long_word[location:location+second_location+2])

Output:

在此输入图像描述

There are a million ways to approach this. One, which is a bit ugly but interesting for learning is to slice your string such as: mystring[start:stop] where you specify the start point as the first .find() and the stop point as the second .find().

The stop point is interesting, because you're passing .find()+1 as the start point of .find() so it skips the first instance of the letter. The final +1 is to include the 't' in the output if you want it.

Typically in python this would be frowned upon because it's unnecessarily unreadable, but I thought I'd post it to give you an idea of how flexible you can be in solving these problems

long_word[long_word.find('t'):long_word.find('t',long_word.find('t')+1)+1]

Output

'taposit'
def find_all(a_str, sub):
    start = 0
    while True:
        start = a_str.find(sub, start)
        if start == -1: return
        yield start
        start += len(sub) # use start += 1 to find overlapping matches


long_word = "juxtaposition"
location = "t"
locations = (list(find_all(long_word, location)))
substr = long_word[locations[0]:locations[1]+1]
print (substr)

output:

taposit

The find method on strings in Python accepts a second parameter for the index in the string to begin searching. In order to find the second occurrence of the substring's index, provide the first occurrence's index + 1 as the second parameter to find :

def get_substring(long_word, search):
    first_occurence_idx = long_word.find(search)

    if first_occurence_idx == -1:
        return

    # for second call of `find`, provide start param so that it only searches
    # after the first occurence
    second_occurence_idx = long_word.find(search, first_occurence_idx + 1)

    if second_occurence_idx == -1:
        return

    return long_word[first_occurence_idx:second_occurence_idx + len(search)]

# example provided
assert get_substring('juxtaposition', 't') == 'taposit'

# case where search occurs once in long_word
assert get_substring('juxtaposition', 'x') is None

# case where search is not in long_word
assert get_substring('juxtaposition', 'z') is None

# case where len(search > 1) and search occurs twice
assert get_substring('juxtaposition justice', 'ju') == 'juxtaposition ju'

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