-1

The problem:

Given a string in which the letter h occurs at least twice. Remove from that string the first and the last occurrence of the letter h, as well as all the characters between them.

How do I find the first and last occurrence of h? And how can I remove them and the characters in between them?

#initialize the index of the input string
index_count =0

#create a list to have indexes of 'h's
h_indexes = []

#accept input strings
origin_s = input("input:")

#search 'h' and save the index of each 'h' (and save indexes of searched 'h's into h_indexes
for i in origin_s:

first_h_index =
last_h_index = 

#print the output string
print("Output:"+origin_s[     :     ]+origin_s[     :])
Bogdan Doicin
  • 2,342
  • 5
  • 25
  • 34
Ma Tabe
  • 11
  • 1
  • 1
  • 2
    The title and the content of the question do not quite say the same thing – DeepSpace Jun 02 '19 at 12:44
  • Welcome to SO. Please take the time to read [ask] and the other links found on that page. – wwii Jun 02 '19 at 14:06
  • This question is definitely a duplicate; you should spend the first 5 min (at least) of any session on SO trying to find if the question has been asked already. Not only will this give you an opportunity to collect URLs when you're comparing to "what's already been asked", it will also give you a better sense for how to ask your question in a clear and unique way, as well as whittle down your reproducible example to its most basic parts. – d8aninja Jun 02 '19 at 14:15
  • Possible duplicate of [regex to remove the last word if it contains a character](https://stackoverflow.com/questions/13742515/regex-to-remove-the-last-word-if-it-contains-a-character) – d8aninja Jun 02 '19 at 14:18
  • 1
    Possible duplicate of [Removing from a string all the characthers included between two specific characters in Python](https://stackoverflow.com/questions/48357619/removing-from-a-string-all-the-characthers-included-between-two-specific-charact) – dpant Jun 02 '19 at 14:39

5 Answers5

2

Using a combination of index, rindex and slicing:

string = 'abc$def$ghi'
char = '$'
print(string[:string.index(char)] + string[string.rindex(char) + 1:])
# abcghi
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
1

You need to use regex:

>>> import re
>>> s = 'jusht exhamplhe'
>>> re.sub(r'h.+h', '', s)
'juse'
Dmitrii K
  • 249
  • 2
  • 13
  • 3
    Well, you don't _need_ to. Locating the first and last occurrence explicitly and then slicing the rest would work as well. – Roland Weber Jun 02 '19 at 12:46
1

How do I find the first and last occurrence of h?

First occurence:

first_h_index=origin_s.find("h");

Last occurence:

last_h_index=origin_s.rfind("h");

And how can I remove them and the characters in between them?

Slicing

Bogdan Doicin
  • 2,342
  • 5
  • 25
  • 34
0
string = '1234-123456789'
char_list = []
for i in string:
    char_list.append(string[i])

char_list.remove('character_to_remove')

According to the documentation, remove(arg) is a method acting on a mutable iterable (for example list) that removes the first instance of arg in the iterable.

Eric Jin
  • 3,836
  • 4
  • 19
  • 45
  • 1
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – rene Jun 02 '19 at 15:36
0

This will help you to understand more clearly:

string = 'abchdef$ghi'

first=string.find('h')

last=string.rfind('h')

res=string[:first]+string[last+1:]

print(res)