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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With