Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # *Letters and Number Triangle Pattern* by @CodingComputing
- from string import ascii_uppercase as alphas # Alphabets
- from string import digits # String digits 0...9
- natural_nums = digits[1:] # String digits 1...9, because pattern starts from 1
- height = 5 # height of the pattern
- max_width = 2*height + (2*height-1)
- # `height` number of alphabets, plus `height` number of digits,
- # plus `2*height-1` number of spaces between characters
- for row_idx in range(height):
- # Slice starting from row_idx, of (row_idx+1) items ahead
- row_slice = slice(row_idx, 2*row_idx+1)
- row_alphas = alphas[row_slice] # slice out from alphas
- row_digits = natural_nums[row_slice] # slice out from digits
- row_chars = row_alphas + row_digits # characters to be printed in current row
- row_content = ' '.join(list(row_chars)) # insert spaces between chars
- print(f"{row_content:^{max_width}}") # format to centre at max_width
- # Output:
- # A 1
- # B C 2 3
- # C D E 3 4 5
- # D E F G 4 5 6 7
- # E F G H I 5 6 7 8 9
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement