Python program to read input from console

PythonServer Side ProgrammingProgramming

Suppose we have to take firstname and lastname from console and write a prompt like "Hello <firstname> <lastname>, you are welcome!". To get the result we can use the format() class.We can placeholder into the string using {}, then pass arguments into format() function.

So, if the input is like Ashish Dutta, then the output will be "Hello Ashish Dutta, you are welcome!"

To solve this, we will follow these steps −

  • fn := take first input from the console

  • ln := take second input from the console

  • ret := "Hello a {} {}, you are welcome!" then call format with this string like format(fn, ln)

  • return ret

Example

Let us see the following implementation to get better understanding

def solve():
   fn = input()
   ln = input()
   ret = "Hello {} {}, you are welcome!".format(fn, ln)
   return ret

print(solve())

Input

Ashish
Dutta

Output

Hello Ashish Dutta, you are welcome!
raja
Updated on 11-Oct-2021 10:42:01

Advertisements