pattern A AB ABC ABCD ABCDE in Python
Question
pattern A AB ABC ABCD ABCDE in Python
Solution
Sure, here is a Python code snippet that generates the pattern you specified:
def print_pattern(n):
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i in range(n):
for j in range(i+1):
print(alphabet[j], end="")
print()
print_pattern(5)
This code works as follows:
- It defines a string
alphabetthat contains all the uppercase letters of the English alphabet. - The
print_patternfunction takes an integernas an argument, which specifies the number of lines to print. - The outer loop
for i in range(n)runsntimes. For each iteration, it prints a new line of the pattern. - The inner loop
for j in range(i+1)runsi+1times. For each iteration, it prints thej-th letter of the alphabet. print()is used to print a newline after each line of the pattern.- Finally,
print_pattern(5)is called to print the pattern with 5 lines.
Similar Questions
correct answerWhat will be the output of the following Python code?print('*', "abcde".center(6), '*', sep='')
Write a Python program that matches a string that has an 'a' followed by anything, ending in 'b'
Select the correct answerWhat will be the output of the following Python code?import rep = 'abc123 xyz789 lmn-22 def456'print(re.sub(r'\b([a-z]+)(\d+)', r'\2\1:', p))Options‘456def: lmn-22: 789xyz: 123abc’‘abc123: xyz789: lmn-22: def456’‘abc123:’, ‘xyz789:’, ‘lmn-22:’, ‘def456:’‘123abc: 789xyz: lmn-22 456def:’
What will be the output of following?x = ‘abcd’for i in range(len(x)): print(x[i])
answerWhat will be the output of the following Python code snippet?print('12abcdeabcopabc'.split('abc', 0))
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.