Python how to add char to a string every x position -


i'm trying write function returns string every xth character followed asterisk.

string_chunks("once upon time, in land far far away, 5)

should return:

'o*once u*pon a* time*, in lan*d far far* away*' 

here code far:

def string_chunk(string, x):     strings = ""     y = x     char in string:         strings += string[y-x:y] + "*"         y += x     return strings    print string_chunk("summer cool", 5) 

this get. dont want added asterisks @ end.

"summe*r *reall*y coo*l*****************" 

you use list comprehension , join tha list asterisk:

astr = 'this example string' '*'.join([astr[j*5:j*5+5] j in xrange(len(astr)/5+1)]) ## 'this *is an* exam*ple s*tring*'  astr = 'this example string too' '*'.join([astr[j*5:j*5+5] j in xrange(len(astr)/5+1)]) ## 'this *is an* exam*ple s*tring* too'     

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -