How can I write two letters with many spaces between them in Python?

Viewed 54

I'm trying to make a pattern looking like this: click here, it's a capital A made out of A's

I'm running into trouble getting the A's to space apart by four or five spaces (A____A). I have to use the format method, but I have no idea how to go about doing that. What is the easiest way? I tried to do something like this, but it says invalid syntax and I am very confused about how to do it right.

print('A',{:4d}.format('A'))
2 Answers

You need to wrap the format in a string, as in '{:4d}'.format(42).

Note that the d specifier expects an integer to be formatted, so '{:4d}'.format('A') would raise an error since 'A' is a string, not an int.

Instead, you remove the d:

print('A{:>4}'.format('A'))  # or print(f'A{"A":>4}')
# A   A

The > will right-align the 'A', so that the spaces go in the middle.


A simpler way, without bothering with the formatting mini-language, is to simply insert the spaces:

print('A' + ' '*3 + 'A')

You can try

print(f"{A:04}")

Related