Search code examples
pythonpython-3.xalphabetical

Arranging alphabets in a right angled triangle


I want to arrange alphabets in a right-angle triangle. One example is:- inp='''1''' out=_A_, inp='''3''' out= _A_\n_A_B_\n_A_B_C_ . This is what I have tried so far:-

def letter_range(start, end):
    for i in range(start, end):
        # inner loop
        for j in range(65, i + 1):
            print(f"_{chr(j)}_", end = "")
        print()

def main():

    x = int(input())
    y = int(65 + x)
    # calling Function
    letter_range(65, y)


     

if __name__ == "__main__":
    main()

#Results
inp = 3
out:-
_A_
_A__B_
_A__B__C_

I just want single underscores after and before each alphabet. Pls help


Solution

  • Try replacing

    for j in range(65, i + 1):
            print(f"_{chr(j)}_", end = "")
        print()
    

    with

    for j in range(65, i + 1):
            print(f"_{chr(j)}", end = "")
        print('_')