#Write a function called align_right. align_right should #take two parameters: a string (a_string) and an integer #(string_length), in that order. # #The function should return the same string with spaces #added to the left so that the text is "right aligned" in a #string. The number of spaces added should make the total #string length equal string_length. # #For example: align_right("CS1301", 10) would return the #string " CS1301". Four spaces are added to the left so #"CS1301" is right-aligned and the total string length is #10. # #HINT: Remember, len(a_string) will give you the number of #characters currently in a_string.

Respuesta :

Answer:

This program is written using Python Programming Language;

Comments are not used; however, see explanation section for detailed line by line explanation

See Attachment

Program starts here

def align_right(a_string,string_length):

     if len(a_string) > string_length:

           print(a_string)

     else:

           aligned=a_string.rjust(string_length)

           print(aligned)

   

a_string = input("Enter a string: ")

string_length = int(input("Enter string length: "))

align_right(a_string,string_length)

Explanation:

Function align_right is declared using on line

def align_right(a_string,string_length):

This line checks if the length of the current string is greater than the input length

     if len(a_string) > string_length:

If the above condition is true, the input string is printed as inputted

           print(a_string)

If otherwise,

     else:

The string is right justified using the next line

           aligned=a_string.rjust(string_length)

The aligned string is printed using the next line

           print(aligned)

   

The main function starts here

This line prompts user for a string

a_string = input("Enter a string: ")

This line prompts user for total string length

string_length = int(input("Enter string length: "))

This line calls the defined function

align_right(a_string,string_length)

Ver imagen MrRoyal
ACCESS MORE
ACCESS MORE
ACCESS MORE
ACCESS MORE