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
Share
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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
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.