1.如果是對某個確定的字符或者數字進行判斷,可以直接使用endswith()方法
# 判斷str_a是否以‘A’結尾 str_a = '20190813A' print(str_a.endswith('A')) # True
2.如果是對不確定的字母或者數字進行判斷,則可以借助python的re模塊
import re
# 判斷字符串是否以字母結尾 def string_compiler(str): text = re.compile(r".*[a-zA-Z]$") if text.match(str): return True else: return False str_b = '20190813' str_c = '20190813abc' print(string_compiler(str_b)) # False print(string_compiler(str_c)) # True
對數字進行判斷只需要修改對應的驗證方式即可