Python Forum
[Solved] How to print a dot leader.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] How to print a dot leader.
#1
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
Reply
#2
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
BashBedlam likes this post
Reply
#3
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
BashBedlam likes this post
Reply
#4
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
BashBedlam likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#5
Great answers! Thank you all.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Print an int with a string Panda 2 4,134 Jun-09-2018, 12:46 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020