First I apologize if this is a question that has been answered many times, I searched around a while for an answer, but much of what I found went over my head since I am just beginning to learn python. But here's my question:
I want to print periods until the 20th character or column of the row so that the data to the right of the periods all line up as shown below.
---------------
HDL
---------------
//============================================================
// USERID:........ ADET_ID
// PROGRAMMER:.... Lname, Fname MI.
// COURSE:........ CSCI-400
// TERM:.......... SP14
// PROJECT:....... 01
// FILENAME:...... AnExample.hdl
//============================================================
Here's what I currently have but how would I account for the periods without actually hard coding them in?
print("//", end = "")
for num in range (0,self.dividerLength):
print("=", end = "")
print("\n", end = "")
print("// USERID:")
print("// PROGRAMMER:")
print("// COURSE:")
print("// TERM:")
print("// PROJECT:")
print("// FILENAME:")
return ""
Use str.ljust:
>>> '// {} {}'.format('USERID:'.ljust(16, '.'), 'ADET_ID')
'// USERID:......... ADET_ID'
More simply using str.format with fill character specified (See Format string syntax):
>>> '// {:.<16} {}'.format('USERID:', 'ADET_ID')
'// USERID:......... ADET_ID'
.: fill character<: left alignment16: width of the result stringWhy 16?
20 - 4
4 = 2 (for //) + 1 (space after //) + 1 (space after .)
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