Skip to main content
Tweeted twitter.com/StackCodeReview/status/1502162417169534985
added 6 characters in body; edited tags
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

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

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

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
edited title
Link
Cody Gray
  • 4.6k
  • 20
  • 30

formatting strings for Python function to output a string that is justified to the right

Became Hot Network Question
format output, reinvent-the-wheel tag
Source Link
AJNeufeld
  • 35.3k
  • 5
  • 41
  • 103

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

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

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

make title describe what code does instead of goals - refer to help center section "Titling your question" on https://codereview.stackexchange.com/help/how-to-ask; update formatting, add tags
Source Link
Loading
edited tags
Link
Mast
  • 13.9k
  • 12
  • 57
  • 128
Loading
Source Link
Loading