如何判断字符串是否在Python中重复出现?
我们要检查我们拥有的字符串是否仅由该字符串的子字符串的重复组成。为了检查我们是否可以检查2个字符串的组合中是否存在字符串旋转。这是因为,当且仅当字符串等于其自身的非平凡旋转时,它才是周期性的。
示例
以下代码对此进行检查并相应地返回:
def find_period(s):
# Concatenate 2 s and find s within
# index one to end of the string
i = (s+s).find(s, 1, -1)
return None if i == -1 else s[:i]
print find_period('012012012012012')
print find_period('some random string')输出结果
这将给我们输出:
012 None
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿