Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python spacing and aligning strings

I am trying to add spacing to align text in between two strings vars without using " " to do so

Trying to get the text to look like this, with the second column being aligned.

Location: 10-10-10-10       Revision: 1 District: Tower             Date: May 16, 2012 User: LOD                   Time: 10:15 

Currently have it coded like this, just using spaces...

"Location: " + Location + "               Revision: " + Revision + '\n' 

I tried working with string.rjust & srting.ljust but to no avail.

Suggestions?

like image 305
Tristan Forward Avatar asked May 16 '12 17:05

Tristan Forward


People also ask

How do you align spaces in Python?

By using String Alignment the output string can be aligned by defining the alignment as left, right or center and also defining space (width) to reserve for the string. Approach : We will be using the f-strings to format the text.

How do you align a string in Python?

Alignment of Strings Using the format() Method in PythonTo left-align a string, we use the “:<n” symbol inside the placeholder. Here n is the total length of the required output string. Left Aligned String with length 10 is: Scaler . To right align a string, we use the “:>n” symbol inside the placeholder.

How do you align text to print in Python?

You can use the :> , :< or :^ option in the f-format to left align, right align or center align the text that you want to format. We can use the fortmat() string function in python to output the desired text in the order we want.

How do you align codes in Python?

Text Alignment You can align values within a specified length of text by using the < , > , or ^ symbols to specify left align, right align, or centering, respectively. Then you follow the those symbols with a character width you desire.


1 Answers

You should be able to use the format method:

"Location: {0:20} Revision {1}".format(Location, Revision) 

You will have to figure out the format length for each line depending on the length of the label. The User line will need a wider format width than the Location or District lines.

like image 154
IronMensan Avatar answered Sep 30 '22 08:09

IronMensan