Introduction:
This Python code demonstrates a simple function that accepts a first name and a last name as parameters. It then concatenates the first name and last name using the + operator and a space to create a full name. Finally, it displays the full name using the print() function.
Code:
#Function to display full name
#The requirements are listed below:
#1. The function should have 2 parameters to accept first name and
#last name.
#2. Concatenate names using + operator with a space between first
#name and last name.
#3. Display full name.
def fullname(first,last):
#+ operator is used to concatenate strings
fullname = first + " " + last
print("Hello",fullname)
#function ends here
first = input("Enter first name: ")
last = input("Enter last name: ")
#function call
fullname(first,last)
Logic:
- Define a function called “fullname” that accepts two parameters: “first” for the first name and “last” for the last name.
- Inside the function, concatenate the first name, a space, and the last name using the + operator. Store the result in a variable called “fullname”.
- Use the print() function to display the full name with a greeting.
- Outside the function, prompt the user to enter their first name and last name using the input() function and store the values.
Output:
The output of this code will be “Hello [Full Name]”. The full name will be displayed by concatenating the first name and last name entered by the user using the + operator with a space between them. The greeting “Hello” will be displayed before the full name.
Enter first name: STEM
Enter last name: Learners
>> Hello STEM Learners