Write A Python Function To Print Hello Plus Each Name In The List

Hi in this article you will learn about a Python function that takes a list  of names as an input argument to it.  The python  function will iterate all the names present in the list of names and print  with the name.

let's try to solve this using the Python program

 

Write A Python Function To Print Hello Plus Each Name In The List



Write A Python Function To Print Hello Plus Each Name In The List

Take a function named say_hello()  and pass a list of names as an input argument to it.  use the iterate all the names present in the list of names and inside the for loop,  use the print function call to print the hello with the name.

def say_hello(names):
   for name in names:
       print(f"Hello, {name}")


In the main program Take a variable named length and use the input function call as the user to enter the list length and then convert it into an integer type.

Take an empty list of names and we will be using this to fill the names entered by the user.  using the for loop iterate 102 the list length and use the input function call to accept the names. Using the built in function up and the  temp item to the list of names.

length = int(input("Enter Number of Names : "))
names_list = []
for _ in range(length):
   temp = input("Enter Each Name : ")
   names_list.append(temp)


say_hello(names_list)

Finally call the function say_hello()  by passing the list  of names as input arguments to the Python function,  as shown above.



Complete Python Program
===============================

def say_hello(names):
   for name in names:
       print(f"Hello, {name}")


length = int(input("Enter Number of Names : "))
names_list = []
for _ in range(length):
   temp = input("Enter Each Name : ")
   names_list.append(temp)

say_hello(names_list)





Output 1
======================

Enter Number of Names : 3
Enter Each Name : google
Enter Each Name : yahoo
Enter Each Name : bing
Hello, google
Hello, yahoo
Hello, bing




Output 2
======================

Enter Number of Names : 3
Enter Each Name : betty
Enter Each Name : robin
Enter Each Name : micheal
Hello, betty
Hello, robin
Hello, micheal




Conclusion
===============================

The above Python program uses  a Python function that takes a list of names, then uses the print function call to print all the names present in the list.

In the main program we will be asking the user to enter all the names present in the list and finally call the function.

Execute the above Python function by passing a random list of names as input to it  and note down the result.  

Comment it down below if you have any queries regarding the above  Python function.



Post a Comment

0 Comments