Print the pattern in python
A
B A
B A B
B A B A
B A B A B
pattern is also in attachment
Attachments:
Answers
Answered by
10
The correct pattern would be -
B
B A
B A B
B A B A
B A B A B
The code -
str="BABAB"
n=len(str)
for i in range(n):
print(" "*(n-i),end="")
print(*str[:i+1],sep=" ")
In each row, there are (n - i) number of spaces where 'i' is the row number (starting with 0) and n is the number of rows. Using string multiplication, spaces are added.
Then, using string slicing, alphabets are displayed with a space in between.
Attachments:
anindyaadhikari13:
Thanks for the brainliest ^_^
Similar questions