Respuesta :
Answer:
Here is the Python program:
def format_name(first_name, last_name): #function definition of format_name that takes first_name and last_name as parameters
if first_name != '' and last_name != '': #checks if first_name and last_name both are not equal to " "
return ("Name: " + last_name + ", " + first_name) #returns first name and last_name in format: "Name: last_name, first_name"
elif first_name != '' or last_name != '': #checks if either of the first_name or last_name is not equal to " "
return ("Name: " + last_name + first_name) #returns first name and last_name in format: "Name: last_name" or "Name: first_name"
else: #if both names are blank
return '' #return ""
Explanation:
The working of the above function can be checked by using the following statements:
print(format_name("Ernest", "Hemingway"))
print(format_name("Voltaire", ""))
print(format_name("", "Madonna"))
print(format_name("", ""))
The program has a function format_name which takes two strings i.e. the first_name and last_name as parameters and returns the formatted string.
The first if condition checks if both the first_name and last_name are blank. For example if first_name = "Ernest" and last_name= "Hemingway" this means both the first_name and last_name are not blank or empty. So If this condition evaluates to true then the following statement is returned:
return ("Name: " + last_name + ", " + first_name)
This returns the last_name string first followed by a comma in between and then first_name string in the last. For the above example the string is printed in the following format:
Name: Hemingway, Ernest
The elif condition checks if either of the first_name or last_name is blank. For example if first_name = "Voltaire" and last_name= "" this means last_name is blank or empty. So if this condition evaluates to true then the following statement is returned:
return ("Name: " + last_name + first_name)
This returns the last_name string. For the above example the string is printed in the following format:
Name: Voltaire
IF none of the above condition is true and both the first_name and last_name are blank then the following statement is returned
return ''
This will just print and blank space in output.
The program and its output is attached.
