# HackerRank Solution: Python What's your name? [3 Methods]

Written By - Bashir Alam

## Question: What’s your name? [Python Strings]

You are given the firstname and lastname of a person on two different lines. Your task is to read them and print the following:

``````Hello firstname lastname! You just delved into python.
``````

Function Description

• Complete the `print_full_name` function in the editor below.
• `print_full_name` has the following parameters:
• string first: the first name
• string last: the last name
• Prints string: '`Hello firstname lastname! You just delved into python`' where and are replaced with first and last.

Input Format:

The first line contains the first name, and the second line contains the last name.

Constraints:

The length of the first and last names are each ≤ 10.

Sample Input:

``````Ross
Taylor
``````

Sample Output:

``````Hello Ross Taylor! You just delved into python.
``````

Explanation

The input read by the program is stored as a string data type. A string is a collection of characters.

## Possible solutions

Now we will see various solutions, to solve the problem given above. The following code is already given on the editor of the hacker rank:

``````# Complete the 'print_full_name' function below.
#
# The function is expected to return a STRING.
# The function accepts following parameters:
#  1. STRING first
#  2. STRING last
#

def print_full_name(first, last):

if __name__ == '__main__':
first_name = input()
last_name = input()
print_full_name(first_name, last_name)``````

Let us now jump into solutions:

### Solution-1: Using `{}` brackets

Let us now use the {} brackets to solve the problem:

``````def print_full_name(first, last):
print(f'Hello {first} {last}! You just delved into python.')
if __name__ == '__main__':
first_name = input()
last_name = input()
print_full_name(first_name, last_name)``````

This code defines a function called "`print_full_name`" which takes in two parameters, "`first`" and "`last`", and uses them to create a string that says "`Hello (first) (last)! You just delved into python.`" using string formatting. The function then prints that string.

The solution also includes an if statement that checks if the current script is being run as the main program (as opposed to being imported as a module into another program). If the current script is the main program, it prompts the user to input their first and last name, then calls the "`print_full_name`" function with the user's input as the arguments, and prints the output.

### Solution-2: Using `.format` to print

Now, we will use .format to print and solve the given problem:

``````def print_full_name(first, last):
print(f'Hello {first} {last}! You just delved into python.')
if __name__ == '__main__':
first_name = input()
last_name = input()
print_full_name(first_name, last_name)``````

This solution also provide exactly the same as the previous code you provided. The only difference is the last line of the if block, where the function call is missing the . at the end of the line. This means that the function will execute and output the string as expected, but it will not return any value and this value will not be assigned to any variable.

### Solution-3: Using + operators

We can also use the + operator to connect strings together as shown below:

``````def print_full_name(first, last):
print("Hello " +first+" "+last+"! You just delved into python.")
if __name__ == '__main__':
first_name = input()
last_name = input()
print_full_name(first_name, last_name)``````

This solution uses string concatenation to create and print a string that says "Hello (first) (last)". The code uses the "+" operator to join the string "Hello ", the variable "first", the string " ", the variable "last", and the string ".

## Summary

In this short article, we discussed how we can solve "What's your name?" problem on Hacker Rank. We solved the problem using three different methods and explained each one.

Question on Hacker Rank: What's your Name?

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation. For any other feedbacks or questions you can either use the comments section or contact me form.