0
How do i align texts(strings) in python
i would like to know how i can align my text to the center and to the right without storing it as a variable example; print('Hi welcome\nWe are good here') how do i make it output this way Hi welcome we are good here
4 ответов
+ 8
I think what you're looking for is formatting a string to be printed. For example, you might specify the alignment and reserve a space for x characters.
'{:<30}'.format('text aligned to the left')
will align the string in '' to the left and reserve a space of 30 chars for printing it.
'{:*>14}'.format('right')
will align to the right and reserve 14 chars. Also, it will use an asterisk as a filler :)
+ 3
I was thinking about this and I wrote a new function...
--------------------------------------------
def centerprint(y,x):
x=" "*y + x
x=x.replace('\n', '\n' + " "*y)
print(x)
---------------------------------------------
centerprint(10, 'Hi welcome\nWe are good here')
centerprint(7, 'Line 1\nLine 2\nLine 3\nEtc')
If you use that function, it will move all your lines over however far you say.
instead of print() it is centerprint().. the first argument is the distance you want to move it and the second is your string
+ 1
print('''
Hi welcome
we are good here
''')
0
thanks man but i dont wanna assign the text to a variable
just need it inside a print()
see what i really mean from my question