Greet With a Default Greeting

Easy ⏱ 8 min 86% acceptance ★★★★☆ 4.2
Write a function greet(name, greeting="Hello") that returns the string "{greeting}, {name}!". If the caller does not pass greeting, it should fall back to "Hello".

Examples

Example 1
Input
greet("Amit")
Output
Hello, Amit!
Explanation

No greeting supplied, so the default "Hello" is used.

Example 2
Input
greet("Priya", greeting="Hi")
Output
Hi, Priya!
Explanation

Caller overrides the default greeting.

Constraints

  • name is always a non-empty string.
  • greeting defaults to "Hello" when omitted.

Topics

Functionsdefault arguments

Companies

AmazonTCSInfosys

Hints

Hint 1

Give greeting a default value in the function signature.

Hint 2

Use an f-string to build the result.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.