Python How to output words that start with a specific string and end with a specific string

Asked 2 weeks ago, Updated 2 weeks ago, 2 views

You want to write a program that reads the text file contents and then receives a string to extract words that end with a specific character 2 that starts with a specific character 1. The text file is read, and I'm lost on what to do next. Can I do this using a regular expression? Or is there another way?


2022-09-20 16:24

1 Answers

There's a lot of ways.

It can be implemented through re-module. I can't explain in detail because I don't know the re-module command properly.

It would be helpful if someone else told me or looked up the re-module regular expression.

If you are simply checking the first letter and the end, you can implement it as follows.

a = 'text'
if a[0] == 'tek' and a[-1] == 't':

2022-09-20 16:24

If you have any answers or tips

© 2022 pinfo. All rights reserved.