I am trying to display any string in pyramid shape e.g. if I am to enter 'adil', I want to display it like
a
ad
adi
adil
inp = input('Enter something: ')
l = len(inp)
i=0
while i < l:
for j in range(l):
print(inp[j])
i =1
CodePudding user response:
You can use slicing and format strings:
fmt='{:^' str(len(s) 1) 's}' # format strings centers text
for i in range(1,len(s) 1): print(fmt.format(s[:i]))
Works (optically) better with longer strings
CodePudding user response:
An alternative, this will print it like a pyramid (triangle):
inp = "adil"
for i, l in enumerate(inp):
print(inp[:i 1].center(len(inp)))
Output:
a
ad
adi
adil
Or, remove center
to print like the example in your question:
inp = "adil"
for i, l in enumerate(inp):
print(inp[:i 1])