pythonstring

How to fix Python doesn't print all my string


I write some code, I want to print everything, but in one place the last character is not printed

a = input("Input Text\n")
b = []
i = 0
while i < len(a):
    b.append(a[0:i])
    i = i+1

for c in b:
    print(c)
temp = ''
for i in range(-1, -len(b), -1):
    temp = b[i]
    print(temp)

Example:

input text = 'abc', it will print this

a, ab, ab, a


Solution

  • I think you can rework your initial while loop to fit the second half of your prints

    a = input("Input Text\n")
    b = []
    
    #Take slice from front to back
    i = 0
    while i < len(a):
        #Take slice from first index till the last
        b.append(a[:i+1])
        i = i+1
    
    #Take slice from back to front
    i = len(a)-1
    while i > 0:
        # Take slice from last index till the first
        b.append(a[:i])
        i = i-1
    
    for c in b:
        print(c)
    

    The output will be

    Input Text
    helloworld
    h
    he
    hel
    hell
    hello
    hellow
    hellowo
    hellowor
    helloworl
    helloworld
    helloworl
    hellowor
    hellowo
    hellow
    hello
    hell
    hel
    he
    h