Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing until a specific character position in python

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 ""
like image 414
cjwert Avatar asked Dec 28 '25 15:12

cjwert


1 Answers

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 alignment
  • 16: width of the result string

Why 16?

20 - 4

4 = 2 (for //) + 1 (space after //) + 1 (space after .)

like image 121
falsetru Avatar answered Dec 30 '25 05:12

falsetru