用python打印一个单词
你好,我想做一个函数,将使用增强(这只是改变了单词已经做了),并打印在一个给定的数字n的部分新词。S=test的示例我应该得到(“#t”,“#te”,“tes”,“est”,“st%”和“t%”)用python打印一个单词,python,regex,printing,substitution,Python,Regex,Printing,Substitution,你好,我想做一个函数,将使用增强(这只是改变了单词已经做了),并打印在一个给定的数字n的部分新词。S=test的示例我应该得到(“#t”,“#te”,“tes”,“est”,“st%”和“t%”) def增强(S,n): S=“#”*(n-1)+S+“%”*(n-1) 返回S def爆炸器(S、n): S=增强(S,n) x=0 对于范围内的i(n1个立即修复,而不是: for i in range (n <= len(S)): 这将为您提供范围n内的i值 输出: ##t #te t
def增强(S,n):
S=“#”*(n-1)+S+“%”*(n-1)
返回S
def爆炸器(S、n):
S=增强(S,n)
x=0
对于范围内的i(n1个立即修复,而不是:
for i in range (n <= len(S)):
这将为您提供范围n内的i值
输出:
##t
#te
tes
est
st%
t%%
还有一个问题在哪里?看起来类似于:但这样我就不会打印最后一个。@Alex oops,没有注意到。你只需要像我一样在I
的范围内添加一个。这很有效,但我再打印一个。对不起,我是新手python@Alex只需调用exploder
,不要打印它,因为您正在打印它的内容eturns which is nothing.exploder
自行打印。@Alex正确识别了代码的第二个问题。print(exploder(S,n))
是None
输出的来源。
for i in range(n, len(S) + 1):
print(exploder(S,n))
exploder(S,n)
def enhance(S, n):
S = "#" * (n - 1) + S + "%" * (n - 1)
return S
def exploder(S, n):
S = enhance(S, n)
for i in range(len(S)-n+1):
print(S[i:i+n])
S = "test"
n = 3
exploder(S, n)
##t
#te
tes
est
st%
t%%