Write full_name(first, last, title=""), which returns the title, first name and last name separated by spaces, or just the two names when there is no title.
What to build
A function with an optional parameter.
Requirements
- Write
full_name(first, last, title=""). - With a title, return the title, first name and last name separated by spaces.
- Without a title, return only the two names.
Input
Three lines: the first name, the last name, and a title, which may be an empty line.
Output
The full name on one line.
Constraints
The title may be an empty line.
Example
Input:
Rahim
Uddin
Dr.
Output:
Dr. Rahim Uddin
Why it works
A default of an empty string lets one function handle both forms. Testing whether the title is empty avoids a stray space at the start.
Examples
Example 1 · Input
Rahim Uddin Dr.
Expected output
Dr. Rahim Uddin
Example 2 · Input
Nadia Islam
Expected output
Nadia Islam
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
An empty string counts as False in an if.
Hint 2
Join the parts with " " between them.
Solution
Show one correct solution (try it yourself first)
def full_name(first, last, title=""):
if title:
return title + " " + first + " " + last
return first + " " + last
first = input()
last = input()
title = input()
print(full_name(first, last, title))