将英语单词'Apostrophes”翻译为中文'撇号”,'positions”翻译为'位置”,得到中文问题:撇号的位置。 以下是一个包含代码示例的解决方法,用于在字符串中查找所有撇号的位置:
s = "I'm a string with some 'apostrophes' in it"
# Find the index of all apostrophes in the string
apostrophe_positions = [i for i, char in enumerate(s) if char == "'"]
print(apostrophe_positions) # [2, 22]
以上代码使用列表推导式来查找字符串中所有的撇号,并存储它们的索引位置。输出结果为撇号在字符串中的索引位置,即[2, 22]。