How do i align texts(strings) in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

8th Feb 2017, 3:29 PM
Prince Kelvin
Prince Kelvin - avatar
4 Answers
+ 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 :)
8th Feb 2017, 8:42 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 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
8th Feb 2017, 7:19 PM
LordHill
LordHill - avatar
+ 1
print(''' Hi welcome we are good here ''')
8th Feb 2017, 7:10 PM
richard
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
8th Feb 2017, 3:56 PM
Prince Kelvin
Prince Kelvin - avatar