0
Hey guys , i ran this code on "splitting string" code coach chellenge,it work but it's too long. Help me make it shorter ,plzzz
The first input take a string The second input take a number Your mission is to separate it into small part by minus sign so that all parts except the last one have the length equal to the num in second input(the last part can equal or not equal) For example: Input: Heyyo 2 Output He-yy-o This is my code: https://code.sololearn.com/cBqmM6KUHjEy/?ref=app
3 Answers
+ 4
t = input()
n = int(input())
print(*(t[i:i+n] for i in range(0,len(t),n)),sep='-')
+ 4
it's equivalent (but shorter and without intermediate variable) to do:
s = []
for i in range(0,len(t),n):
s.append(t[i:i+n])
print('-'.join(s))
the loop is replaced by a list comprehension
the join is replaced by unpacking the list (comprehension) and use of named argument 'sep', wich overide the default space separator between print() arguments ;)
+ 1
How can you do it??proooooo