可以使用 split() 函数将字符串根据指定的分隔符切割为一个列表,然后通过索引取得最后一个元素。可以按照以下方式实现:

string = 'abc/def/ghi'
last_element = string.split('/')[-1]
print(last_element)

输出:

ghi

在上述代码中,我们将字符串 'abc/def/ghi' 使用 split('/') 函数切割为一个列表 ['abc', 'def', 'ghi'],然后通过索引 [-1] 取得最后一个元素 'ghi'


原文地址: https://www.cveoy.top/t/topic/p8Vt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录