w3resource

Python: Print first and last name in reverse order with a space between them

Python Basic: Exercise-5 with Solution

Reverse Full Name

Write a Python program that accepts the user's first and last name and prints them in reverse order with a space between them.

Python has two functions designed for accepting data directly from the user:

  • Python 2.x. -> raw_input()
  • Python 3.x. -> input()

raw_input():

raw_input() asks the user for a string of data and simply returns the string, the string data ended with a newline). It can also take an argument, which is displayed as a prompt before the user enters the data.

print raw_input('Input your name: ') 

prints out

Input your name: <user input data here>

To assign the user's name to a variable "x" you can use following command :

x = raw_input('Input your name: ') 

input():

In 3.x .raw_input() is renamed to input(). input() function reads a line from sys.stdin and returns it with the trailing newline stripped.

To assign the user's name to a variable "y" you can use following command :

y = input('Input your name: ') 

Pictorial Presentation:

Print first and last name in reverse order with a space between them

Sample Solution:

Python Code:

# Prompt the user to input their first name and store it in the 'fname' variable
fname = input("Input your First Name : ")

# Prompt the user to input their last name and store it in the 'lname' variable
lname = input("Input your Last Name : ")

# Display a greeting message with the last name followed by the first name
print("Hello  " + lname + " " + fname)

Sample Output:

Input your First Name : Dany                                       
Input your Last Name : Boon                                        
Hello  Boon Dany              

Explanation:

This code prompts the user to input their first and last name, and then it prints a message that combines the first name and last name in a specific order.

  1. The first line fname = input("Input your First Name : ") gets the user's first name using the input() function and assigns it to the variable 'fname'.
  2. The second line lname = input("Input your Last Name : ") gets the user's last name using the input() function and assigns it to the variable 'lname'.
  3. The third line print ("Hello " + lname + " " + fname) prints a message that combines the last name and first name, concatenating them with the + operator and separated by a space.

Flowchart:

Flowchart: Print first and last name in reverse order with a space between them.

Python Code Editor:

 

Previous: Write a Python program which accepts the radius of a circle from the user and compute the area.
Next: Write a Python program which accepts a sequence of comma-separated numbers from user and generate a list and a tuple with those numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/python-basic-exercise-5.php