0

Write a Python program to find all words starting with 'a' ending with 'e' in a given string inp = "abce abde test xabe"

Write a Python program to find all words starting with 'a' ending with 'e' in a given string inp = "abce abde test xabe"

15th Sep 2022, 6:08 PM
Pavan Kuncham
7 Antworten
+ 5
Hassanah , we appreciate your support, but it is not seen as a helpful behavior if we post ready-made codes as long as the op has not posted his attempt. in this situation it is better to give hints.
16th Sep 2022, 5:45 PM
Lothar
Lothar - avatar
+ 5
Daniel C , we can avoid splitting the string, if we use *word boundary* instead of *string boundary*: "\ba\w+e\b"
16th Sep 2022, 5:56 PM
Lothar
Lothar - avatar
+ 3
If you need regex, you should still split the string at each space. Then, you can test with the following regular expression: ^a.*e$ That expression means to check for a starting letter 'a', then any amount of any character, ending with 'e'. (tested at https://regexr.com/ )
15th Sep 2022, 6:43 PM
Daniel C
Daniel C - avatar
+ 3
import regex wordsWithAAndE = regex.findall(r'\b[a]\w+[e]\b', inp, regex.I) print(wordsWithAAndE) not sure if this is what you're looking for, but I tried to find a solution to your problem, hope it helps :)
15th Sep 2022, 6:45 PM
Hassanah
Hassanah - avatar
+ 2
We won't do your homework for you, but I'll outline how this program should work to guide you in the right direction. Once you get the input, split it at every space character (the string.split method). This will return a list of each word. For each entry in the list, check if the first letter is 'a' and if the last letter is 'e'. In Python, the first index is accessed using [0] and the last index is accessed using [-1].
15th Sep 2022, 6:15 PM
Daniel C
Daniel C - avatar
+ 2
Lothar, Ok I'll be mindful next time :)
17th Sep 2022, 10:14 AM
Hassanah
Hassanah - avatar
0
Daniel C I want it using regex
15th Sep 2022, 6:23 PM
Pavan Kuncham