Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python input() Function
Next article icon

In this post, We will see how to take integer input in Python. As we know that Python's built-in input() function always returns a str(string) class object. So for taking integer input we have to type cast those inputs into integers by using Python built-inint() function.

Let us see the examples:

Example 1:

Python
# take input from userinput_a=input()# print data typeprint(type(input_a))# type cast into integerinput_a=int(input_a)# print data typeprint(type(input_a))

Output:

100
<class 'str'>
<class 'int'>

Example 2:

Python
# string inputinput_a=input()# print typeprint(type(input_a))# integer inputinput_b=int(input())# print typeprint(type(input_b))

Output:

10
<class 'str'>
20
<class 'int'>

Example 3:

Python
# take multiple inputs in arrayinput_str_array=input().split()print("array:",input_str_array)# take multiple inputs in arrayinput_int_array=[int(x)forxininput().split()]print("array:",input_int_array)

Output:

10 20 30 40 50 60 70
array: ['10', '20', '30', '40', '50', '60', '70']
10 20 30 40 50 60 70
array: [10, 20, 30, 40, 50, 60, 70]

Example 4:

Python
# Python program to take integer input  in Python# input size of the listn=int(input("Enter the size of list : "))# store integers in a list using map, split and strip functionslst=list(map(int,input("Enter the integer elements of list(Space-Separated): ").strip().split()))[:n]print('The list is:',lst)# printing the list

Output:

Enter the size of list : 4
Enter the integer elements of list(Space-Separated): 6 3 9 10
The list is: [6, 3, 9, 10]

Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp