Sie*_*de 6 python string substring repeat
我在Python中寻找一个函数,你在其中给一个字符串作为输入,其中某个单词已被重复多次,直到达到一定的长度.
然后输出就是那个词.重复的单词在整体上没有必要重复,也可能根本没有重复.
例如:
"pythonpythonp" => "python"
"hellohello" => "hello"
"appleapl" => "apple"
"spoon" => "spoon"
Run Code Online (Sandbox Code Playgroud)
有人能给我一些关于如何编写这种功能的提示吗?
您可以通过重复子字符串一定次数并测试它是否等于原始字符串来完成此操作.
除非你将其保存为变量,否则你必须为每个可能的字符串长度尝试它
这是代码:
def repeats(string):
for x in range(1, len(string)):
substring = string[:x]
if substring * (len(string)//len(substring))+(substring[:len(string)%len(substring)]) == string:
print(substring)
return "break"
print(string)
repeats("pythonpytho")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12442 次 |
| 最近记录: |