A few questions about string formating in python. Alignment

Sandzhaj :

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?

Mace :

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=407807&siteId=1