2016-01-29 237 views

回答

1

您可以去除标点符号和拆分文本(含空格str.split()方法的默认参数),然后使用一个索引来获得最后一个字:

>>> import string 
>>> x = 'hello, what is your name?' 
>>> 
>>> x.strip(string.punctuation).split()[-1] 
'name' 

有使用正则表达式的另一种方式,我不推荐这项任务:

>>> import re 
>>> re.search(r'\b(\w+)\b\W$',x).group(1) 
'name' 
相关问题