Python打印指定字符图案的替代实现方案咨询
Nice work getting that pattern printed! Your existing code does the job, but let's explore a few cleaner, more readable approaches that might make maintaining or modifying the pattern easier later on.
First, let's recap the pattern we're targeting: starting with A B C D E F G F E D C B A, then A B C D E F F E D C B A, and so on until we end with A A.
Your Original Implementation
Just for reference, here's the code you shared:
n=0 for i in range(71,64,-1): for j in range(65,i+1): a=chr(j) print(a, end=" ") if n>0: for l in range(1,3+(n-1)*4): print(end=" ") if i<71: j=j+1 for k in range(j-1,64,-1): b=chr(k) print(b, end=" ") n=n+1 print()
Approach 1: String Construction + Center Alignment
This method focuses on building each line's character sequence first, then letting Python handle the spacing automatically with string alignment. It's much more readable because we separate the logic of creating the characters from formatting the line.
# Define the highest character we start with (G in your case) max_char = 'G' max_ord = ord(max_char) # Calculate the width of the longest line (to center all lines properly) longest_line_length = 2 * (max_ord - ord('A')) * 2 + 1 # For G: 2*6*2 +1 =25 for current_ord in range(max_ord, ord('A') -1, -1): # Build the forward sequence: A to current character forward = [chr(c) for c in range(ord('A'), current_ord +1)] # Build the backward sequence: current character -1 down to A backward = [chr(c) for c in range(current_ord -1, ord('A') -1, -1)] # Combine and convert to a space-separated string line = ' '.join(forward + backward) # Center-align the line to match the longest line's width print(line.center(longest_line_length))
Why this works:
- We first create the full character list for each line (forward + backward) instead of printing piecemeal.
str.center()automatically adds the correct number of spaces on both sides to make all lines match the longest line's width—no need to manually calculate space counts like in your original code.- It's easy to adjust the pattern (e.g., change the starting character from G to Z) by just modifying
max_char.
Approach 2: Explicit Loop with Space Calculation
If you prefer a more hands-on approach that mirrors your original logic but simplifies the space math, this version breaks down the spacing calculation into a clear formula:
start_char = 'A' end_char = 'G' # Total number of lines (from G down to A) total_lines = ord(end_char) - ord(start_char) + 1 for line_num in range(total_lines): current_end_ord = ord(end_char) - line_num # Print the left half: A to current end character for c in range(ord(start_char), current_end_ord +1): print(chr(c), end=' ') # Calculate the number of spaces needed between left and right halves # Each line up reduces the space by 4 (since we lose two characters, each with a space) spaces = 4 * line_num print(' ' * spaces, end='') # Print the right half: current end character -1 down to A for c in range(current_end_ord -1, ord(start_char) -1, -1): print(chr(c), end=' ') print()
Why this works:
- We calculate the spaces using a simple linear formula (
4 * line_num) instead of nested loops for spacing. This makes it easier to see how the spacing changes per line. - The loop structure is straightforward: left half → spaces → right half, which maps directly to how the pattern looks visually.
Approach 3: Recursive Printing
For a more functional take, we can use recursion to print lines from the top down, stopping when we reach the final A A line:
def print_pattern(current_char, max_char): # Build the line for current_char forward = [chr(c) for c in range(ord('A'), ord(current_char)+1)] backward = [chr(c) for c in range(ord(current_char)-1, ord('A')-1, -1)] line = ' '.join(forward + backward) # Calculate width based on max_char max_width = 2 * (ord(max_char) - ord('A')) * 2 +1 print(line.center(max_width)) # Recurse if we haven't reached 'A' yet if current_char != 'A': print_pattern(chr(ord(current_char)-1), max_char) # Start with the highest character print_pattern('G', 'G')
Why this works:
- Recursion lets us avoid explicit loops by repeating the same logic for each character down to
A. - It's a clean way to model the pattern's repetitive structure—each line is just a smaller version of the one above it.
These approaches all achieve the same result, but each has its own strengths. The string alignment method is great for readability and flexibility, the explicit loop is good for understanding low-level spacing, and recursion is a fun functional approach.
内容的提问来源于stack exchange,提问作者rahul verma

