DEV Community

Cover image for Getting input in python
Aswin Barath
Aswin Barath

Posted on • Edited on

2 2 1 1 1

Getting input in python

Getting input from the user is what makes a program more interactive with the user.

Hence, in python we have an input function: input(), to receive input from the user.

Take a look at an example.
Alt Text
Output:

Enter any data:Happy new year!
Happy new year!
Enter fullscreen mode Exit fullscreen mode

input function under the hood

  • When input() function is encountered by python, the program will pause until the user enters data.
  • Later, any input entered by the user will be converted into a string. Let's see another example to understand this point.

Alt Text
Output:

Enter any text: Have a great year ahead.
text: Have a great year ahead. , type: <class 'str'>
Enter any number: 2021
number: 2021 , type: <class 'str'>
Enter fullscreen mode Exit fullscreen mode
  • So, in these cases make sure that you convert the input to your preferred data type using its corresponding constructors. Let's see that example too.

Alt Text
Output:

Enter any number: 2021
number: 2021 , type: <class 'int'>
Enter fullscreen mode Exit fullscreen mode

Best Resources

Who Am I?

I’m Aswin Barath, a Software Engineering Nerd who loves building Web Applications, now sharing my knowledge through Blogging during the busy time of my freelancing work life. Here’s the link to all of my socials categorized by platforms under one place: https://linktr.ee/AswinBarath

Thank you so much for reading my blog🙂.

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs