Python如何在字符串中找到第二个大写字母的索引



我想获得任何字符串中第二个大写字母的索引。

例如,在字符串"Paul喜欢冰淇淋"中,我想得到"I"的索引,即11。不过,它应该适用于任何字符串。

您可以使用:

m = re.search(r'^([^A-Z]*[A-Z]){2}', 'Paul likes Ice cream.');
print m.span()[1]
12

类似的东西

>>> string = " Paul likes Ice cream." 
>>> [ match.start() for match in re.finditer ("[A-Z]", string) ][1]
12

最新更新