-1

If the verb ends in e, drop the e and add -ing.

I'm inputing a string (English verb). And my goal is to delete last char of the word if it's "e". And add 3 more characters "i","n" and "g".

I'd like to know how to delete the list object or if possible a string character. And how to switch a list into a string.

Currently im on.

if verb_list[-1] == ["e"]:              #verb_list is a inputed string putted into a list
    verb_list[-1] = "i"
    verb_list.append("n")
    verb_list.append("g")

This isnt a proper solution for me. I'd like to know how to delete for example [-1] element from list or from string. Also here im left with a list, and i want my output to be a string.

Thanks for any help!

h0ax
  • 51
  • 6

3 Answers3

2

This should suffice

if verb[-1]=='e':
    verb = verb[:-1]+"ing"

For more about slicing in Python - Understanding slice notation

Saharsh
  • 1,056
  • 10
  • 26
  • What that means? verb[:-1] – h0ax Sep 24 '19 at 09:53
  • @h0ax It removes the last character of a string. A more 'normal' example would be [0:4]. That takes the first 4 characters of a string. Minus means that instead of countring from the front it starts counting from the end. So -1 is the last character. So [:-1] can also be written as [0:-1] which means, take everything from the string from the start until the last character. – Landcross Sep 24 '19 at 09:57
  • Thanks a lot, now i understand well! – h0ax Sep 24 '19 at 10:04
  • @h0ax Have updated answer so you can see how slicing works – Saharsh Sep 24 '19 at 10:11
2

You can use re.sub:

re.sub('e$', 'ing', s)

The $ in the regex matches the pattern only if it's at the end of a string.

Example usage:

import re

data = ['date', 'today', 'done', 'cereal']

print([re.sub('e$', 'ing', s) for s in data])
#['dating', 'today', 'doning', 'cereal']

I know the words in data aren't verbs but those were words off the top of my head.

Jab
  • 26,853
  • 21
  • 75
  • 114
1

Try this:

li=list(verb)
if li[-1]=='e':
    li[-1]='ing'
    verb=''.join(li)
Bharat Gera
  • 800
  • 1
  • 4
  • 13
  • Thats also a nice and simple solution. Can you tell me how ''.join() exactly working ? – h0ax Sep 24 '19 at 10:06
  • The join() method takes all items in an iterable and joins them into one string. Syntex: string_name.join(iterable) . For example: list1 = ['1','2','3','4'] '-'.join(list1) Result would be: 1-2-3-4 – Bharat Gera Sep 24 '19 at 10:08
  • How join works in python, please follow the link: https://www.programiz.com/python-programming/methods/string/join – Bharat Gera Sep 24 '19 at 10:50
  • @h0ax upvote it, if it helps in your case so that others also get the benefit. – Bharat Gera Sep 25 '19 at 05:25