-2

I'm trying to get the two last letters of each word in a text file containing a lot of text. And I'm supposed to use slice notations or something similar to solve this. Say the text is this: " I wanna learn how to slice up letters of each of these words and make a list out of to print when running the program."

And I want to slice up each word and print out the last two letters of each word. Any help is appreciated, thanks in advance. Hopefully..

2 Answers2

0
yourFile = file("yourFile.txt").read()
for word in yourFile.split():
    print word[-2:]
0

Since your data does not give me any clue about the structure of the file. I will assume that all words are spearated by non-digits and non-alphabetic character.

So my code will be

with open(.... , 'r') as file:
      for line in file:
          word_list = re.split('\W+',line)
          for word in word_list:
                print word[-2:]

This should work better.

manugupt1
  • 2,337
  • 6
  • 31
  • 39