检查存储在 python 列表中的单词的最后两个字母

checking the last two letters in a word stored in list in python

in Python 如果 q[n] 是存储在列表 q 中的单词,我想检查这个单词的最后两个字母是否等于某些字符。如何解析单词的字符??

有帮助link: https://thispointer.com/python-how-to-get-last-n-characters-in-a-string/

就像上面说的,你可以对列表进行切片,抓取单词,然后使用负索引位置对单词进行切片,如果与你想要的字符相同,则打印字符。

代码示例:

list = ['abc']  # Create a list containing a word: abc
word = list[0]  # sets variable word to the first word in list
last2 = word[-2:]  # gets last 2 characters, and sets it to the variable
if last2 == 'bc':  # if the last 2 characters are 'bc':
    print(last2)  # prints last 2 characters