+ 1

Can someone help me shorten this?

This is my first real program outside of the stuff I made for my lessons. Is there a way to shorten it? Thanks https://code.sololearn.com/cK7JdO092WDb/?ref=app

18th Jul 2021, 2:37 AM
Alex
Alex - avatar
10 ответов
+ 3
Alex Enter any length of string, doesn't matter a = input() x = len(a) for i in range(1, x + 1): print("*", a[0 : i], "* ") print("\nThat was rather underwhelming, wasn't it? Oh well, you can still leave a like.")
18th Jul 2021, 2:42 AM
A͢J
A͢J - avatar
+ 2
This is a bit shorter (I see it's pretty much what Calvin Thomas said) for i in range(len(a := input())): print("*", a[:i], "*") print("\nThat was rather underwhelming, wasn't it? Oh well, you can still leave a like.")
18th Jul 2021, 6:09 AM
David Ashton
David Ashton - avatar
+ 2
Thanks Calvin Thomas good points.
18th Jul 2021, 7:14 AM
David Ashton
David Ashton - avatar
+ 1
Thank you very much
18th Jul 2021, 3:02 AM
Alex
Alex - avatar
+ 1
Here's a one-liner that might help: for i in range(len(k := input())): print("* ", k[:i+1], " *") # Hope this helps
18th Jul 2021, 5:19 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
David Ashton Your code actually doesn't print the last letter. Also, it prints an unnecessary space character in the beginning. This was why I had added 'i+1' in my code.
18th Jul 2021, 6:23 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
David Ashton You're welcome.
18th Jul 2021, 8:34 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Calvin Thomas it makes total sense, welcome
20th Jul 2021, 11:02 PM
Innocent Salimoni
Innocent Salimoni - avatar
0
Try to create a comprehension list first and iterate through the list with for loop as below: a = input() u = ['* '+ a[0:i] + ' *' for i in range(1,len(a)+1)] for j in u: print(j)
20th Jul 2021, 12:55 AM
Innocent Salimoni
Innocent Salimoni - avatar
0
Innocent Salimoni Why not just a[:i]?
20th Jul 2021, 2:37 AM
Calvin Thomas
Calvin Thomas - avatar