Q:

How to remove the extra whitespace, if one of the print statement doesn't have a middle name?

def get_formatted_name(first_name, last_name, middle_name = ''):
    """Return a full name, neatly formatted."""
    if middle_name:
        full_name = f"{first_name} {middle_name} {last_name}"
    else:
        full_name = f"{first_name} {middle_name} {last_name}"
    return full_name.title()

cricketer = get_formatted_name('sachin', 'tendulkar')
print(cricketer)

cricketer = get_formatted_name('sachin', 'tendulkar', 'ramesh')
print(cricketer)

Output:
Sachin  Tendulkar
Sachin Ramesh Tendulkar

How to remove extra whitespace in first print output because there is no middle name?

A:

Don't include the middle name in the else statement
def get_formatted_name(first_name, last_name, middle_name = ''):
    """Return a full name, neatly formatted."""
    if middle_name:
        full_name = f"{first_name} {middle_name} {last_name}"
    else:
        full_name = f"{first_name} {last_name}"
    return full_name.title()

cricketer = get_formatted_name('sachin', 'tendulkar')
print(cricketer)

cricketer = get_formatted_name('sachin', 'tendulkar', 'ramesh')
print(cricketer)

