Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print a word in parts python

Hello I want to make a function that will use enhance(which just changes the word already made it) and print the new word in parts of a given number n. Example for S=test I should get (‘##t’, ‘#te’, ‘tes’, ‘est’, ‘st%’, ‘t%%’)

def enhance(S,n):
    S = "#"*(n-1)+S+"%"*(n-1)
    return S

def exploder(S,n):
    S = enhance(S,n)
    x=0
    for i in range (n <= len(S)):
        print(S[x:i])
        x=x+1


S="test"
n = 3
for n in range (0,n):
    print(exploder(S,n))
    n=n+1
print(exploder(S,n))
like image 520
Alex Avatar asked Mar 03 '26 22:03

Alex


1 Answers

One immediate fix. Instead of:

  for i in range (n <= len(S)):

I think you want:

  for i in range(n, len(S) + 1):

That will give you values of i in the range n <= i < len(s).

Also, as Alex Hall suggested, change:

print(exploder(S,n))

To just:

exploder(S,n)

The exploder function was returning None. So that print is the source your spurious None outputs.

like image 89
Raymond Hettinger Avatar answered Mar 05 '26 12:03

Raymond Hettinger