I just solved this exercise from Think Python, 2nd edition:
Python provides a built-in function called len that returns the length of a string, so the value of len('allen') is 5. Write a function named right_justify that takes a string named s as a parameter and prints the string with enough leading spaces so that the last letter of the string is in column 70 of the display.
So, I've got these two options that have the same output. Which one is 'better' and why? And are there any good practices among programmers that I'm missing? I'm just starting out and I'd like to avoid bad habits.
Is it better to use .format() to add padding and justify a string or other functions like rjust(), ljust(), center() and zfill()?
1st option
def right_justify(s):
print('{:>70}'.format(s))
right_justify('allen')
2nd option
def right_justify(s):
print(s.rjust(70))
right_justify('allen')
Output(both):
allen
allen