Python Forum

Full Version: [Solved] How to print a dot leader.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Does anyone know of a simpler way to display information with a dot leader?
def add_dot_leader (text) :
	new_text = '' 
	for index in range (1, len (text), 2) :
		thing_one = text [index - 1]
		thing_two = text [index]
		if thing_one == ' ' and thing_two == ' ' :
			new_text += ' .'
		else :
			new_text += thing_one + thing_two
	if len (new_text) < len (text) :
		new_text += text [-1]
	return new_text

record = {'Name': 'Roy Rodgers',
	'Age': 'unknown',
	'Occupation': 'Retired'}

print ()
for label, information in record.items () :
	print (add_dot_leader (f'\t{label:16}{information}'))
Like this :
Output:
Name . . . . . Roy Rodgers Age . . . . . . unknown Occupation . . Retired
Perhaps using the re module
import re

def add_dot_leader(text):
    return re.sub(r' {2}', ' .', text)
Output:
Name . . . . . .Roy Rodgers Age . . . . . . unknown Occupation . . .Retired
filler = '.'
print(f'{"Name ":{filler}<{20}} Ralph')
print(f'{"Age ":{filler}<{20}} 102')
print(f'{"Occupation ":{filler}<{20}} Farmer')
Output:
Name ............... Ralph Age ................ 102 Occupation ......... Farmer
record = {'Name': 'Roy Rogers',
          'Age': 'unknown',
          'Occupation': 'Retired'}
max_label_length = max(record)

for label, information in record.items():
    if len(label) < len(max_label_length):
        space = len(max_label_length) - len(label)+2
    else:
        space = 2
    print(f'\t{label} {"."*space}{information}')
Output:
Name ........Roy Rogers Age .........unknown Occupation ..Retired
Great answers! Thank you all.