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

14th Jun 2021, 3:12 AM
TCorn
TCorn - avatar
3 Antworten
+ 4
t = input() n = int(input()) print(*(t[i:i+n] for i in range(0,len(t),n)),sep='-')
14th Jun 2021, 4:25 AM
visph
visph - avatar
+ 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 ;)
14th Jun 2021, 7:18 AM
visph
visph - avatar
+ 1
How can you do it??proooooo
14th Jun 2021, 7:10 AM
TCorn
TCorn - avatar