Question & Answer

Help your friend write their bibliography! Write the function called citation that takes a tuple like this as input: (“Martin”, “Luther”, “King, Jr.”)

Help your friend write their bibliography! Write the function called citation that takes a tuple like this as input: (“Martin”, “Luther”, “King, Jr.”) and returns a name with the format: Last, First Middle. In this example, the output should be: King, Jr., Martin Luther. Here are some more examples:

citation((“J.”, “D.”, “Salinger”))
# => “Salinger, J. D.”

citation((“Ursula”, “K.”, “Le Guin”))
# => “Le Guin, Ursula K.”

citation((“J.”, “K.”, “Rowling”))
# => “Rowling, J. K.”

1 Answer

Verified archive
Quizzma TeamDecember 13, 2024

Sure! To create a function called `citation` that formats a tuple into the desired name format, you can follow these steps:

1. Accept a tuple with three elements: first name, middle name, and last name.
2. Rearrange these elements into the format: Last, First Middle.

Here’s how you can define the function:


def citation(name_tuple):

first_name = name_tuple[0]

middle_name = name_tuple[1]

last_name = name_tuple[2]

return f"{last_name}, {first_name} {middle_name}"

```

### Explanation:

- The function takes a tuple called `name_tuple`.

- It extracts the first, middle, and last names using indexing.

- It then returns a formatted string using an f-string, which allows you to easily embed expressions inside string literals.

### Example Usage:

```python

print(citation(("J.", "K.", "Rowling"))) # Output: "Rowling, J. K."

You can test this function with the other examples you provided to see that it works as expected! If you need further assistance, feel free to ask.