3

FIRST QUESTION

For example, if i want to print a lot of lines with the same width, i could use

print(f'{"INFO":=^50}')
print(f'{"some info":<50}')
print(f'{"another info":>50}')

And will get

=======================INFO=======================
some info 
 another info

But, what if I want to get something like this?

=======================INFO=======================
some info.............................another info 

Ok. I can do it

print(f'{"INFO":=^50}')
print('some info' + f'{"another info":.>{50-len("some info")}}')

Maybe python has another, the easiest way to do it?

SECOND QUESTION

For align we can use>, <, ^, and = And = works only with numbers. And it works the same as>

For example

print(f'{13:.=5}')
print(f'{13:.>5}')
...13
...13

So Why do we need =, if it works the same? To be sure that the value is a number? What are the pluses it gives more?

asked Apr 4, 2020 at 15:47
0

2 Answers 2

2

For your second question, the answer is in Format Specification Mini-Language:

'='

Forces the padding to be placed after the sign (if any) but before the digits. This is used for printing fields in the form ‘+000000120’. This alignment option is only valid for numeric types. It becomes the default when ‘0’ immediately precedes the field width.

This becomes clear when you have a signed number:

print(f'{-13:0=5}')
# -0013
print(f'{-13:0>5}')
# 00-13
answered Apr 4, 2020 at 15:56
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I should search information more carefully :)
1

What you are trying to do is an alignment inbetween two variables. That's quite specific. What then about alignment between three variables, four etc... ?

You can however approach it as an alignment problem for each of the two variables: split the 50 in two parts.

print(f'{"INFO":=^50}')
print(f'{"some info":.<25}{"another info":.>25}')
=======================INFO=======================
some info.............................another info
answered Apr 4, 2020 at 16:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.