Hướng dẫn what are input methods in python? - phương thức nhập liệu trong python là gì?

Hàm input() lấy đầu vào từ người dùng và trả về nó.

Thí dụ

name = input("Enter your name: ")

print(name) # Output: # Enter your name: James # James


Đầu vào ()

Cú pháp của hàm input() là:

input([prompt])

Thông số đầu vào ()

Hàm input() có một đối số tùy chọn duy nhất:

  • Nhắc (Tùy chọn) - Một chuỗi được ghi vào đầu ra tiêu chuẩn (thường là màn hình) mà không cần theo dõi dòng mới - a string that is written to standard output (usually screen) without trailing newline

đầu vào () giá trị trả về

Hàm input() đọc một dòng từ đầu vào (thường là từ người dùng), chuyển đổi dòng thành một chuỗi bằng cách loại bỏ dòng mới và trả về nó.

Nếu EOF được đọc, nó sẽ tăng ngoại lệ

input([prompt])
2.


Ví dụ 1: Làm thế nào đầu vào () hoạt động trong Python?

# get input from user

inputString = input()

print('The inputted string is:', inputString)

Đầu ra

Python is interesting.
The inputted string is: Python is interesting

Ví dụ 2: Nhận đầu vào từ người dùng với lời nhắc

# get input from user

inputString = input('Enter a string:')

print('The inputted string is:', inputString)

Đầu ra

Enter a string: Python is interesting.
The inputted string is: Python is interesting

❮ Chức năng tích hợp sẵn


Thí dụ

Yêu cầu tên của người dùng và in nó:

print ('Nhập tên của bạn:') x = input () in ('xin chào,' + x)
x = input()
print('Hello, ' + x)

Hãy tự mình thử »


Định nghĩa và cách sử dụng

Hàm input() cho phép đầu vào của người dùng.


Cú pháp

Giá trị tham số

Tham sốSự mô tả
lời nhắcMột chuỗi, biểu thị một thông báo mặc định trước đầu vào.

Nhiều ví dụ hơn

Thí dụ

Yêu cầu tên của người dùng và in nó:

print ('Nhập tên của bạn:') x = input () in ('xin chào,' + x)
print('Hello, ' + x)

Hãy tự mình thử »


❮ Chức năng tích hợp sẵn


Cải thiện bài viết

Lưu bài viết

Hàm Python Input () được sử dụng để lấy đầu vào của người dùng. Theo mặc định, nó trả về đầu vào của người dùng dưới dạng chuỗi. is used to take user input. By default, it returns the user input in form of a string.

Python Input () Chức năng cú pháp

Cú pháp: Đầu vào (Nhắc) input(prompt)

  • Nhắc [Tùy chọn]: Bất kỳ giá trị chuỗi nào để hiển thị dưới dạng thông báo đầu vàoany string value to display as input message

Trả về: Trả về giá trị chuỗi làm đầu vào của người dùng. Return a string value as input by the user.

Ví dụ về hàm Python Input ()

Python3

input([prompt])
4
input([prompt])
5
input([prompt])
6
input([prompt])
7
input([prompt])
8
input([prompt])
9

# get input from user

inputString = input()

print('The inputted string is:', inputString)
0
input([prompt])
7
# get input from user

inputString = input()

print('The inputted string is:', inputString)
2
# get input from user

inputString = input()

print('The inputted string is:', inputString)
3

Output:

What color is rose?: red
Rose is red

Ví dụ 1: Lấy tên và tuổi của người dùng làm đầu vào và in nóthe Name and Age of the user as input and printing it

Theo mặc định, đầu vào trả về một chuỗi. Vì vậy, tên và tuổi sẽ được lưu trữ dưới dạng chuỗi.

Python

# get input from user

inputString = input()

print('The inputted string is:', inputString)
4
input([prompt])
5
input([prompt])
6
input([prompt])
7
# get input from user

inputString = input()

print('The inputted string is:', inputString)
8
input([prompt])
9

Python is interesting.
The inputted string is: Python is interesting
0____15
input([prompt])
6
input([prompt])
7
Python is interesting.
The inputted string is: Python is interesting
4
input([prompt])
9

# get input from user

inputString = input()

print('The inputted string is:', inputString)
0
input([prompt])
7
Python is interesting.
The inputted string is: Python is interesting
8
Python is interesting.
The inputted string is: Python is interesting
9

Output:

Please Enter Your Name: Rohit
Please Enter Your Age: 16
Name & Age:  Rohit 16 

Ví dụ 2: Lấy hai số nguyên từ người dùng và thêm chúng.

Trong ví dụ này, chúng tôi sẽ xem xét cách lấy đầu vào số nguyên từ người dùng. Để lấy đầu vào số nguyên, chúng tôi sẽ sử dụng int () cùng với python input ()Python input()

Python

# get input from user

inputString = input()

print('The inputted string is:', inputString)
4
input([prompt])
5
input([prompt])
6
input([prompt])
7
# get input from user

inputString = input()

print('The inputted string is:', inputString)
8
input([prompt])
9

Python is interesting.
The inputted string is: Python is interesting
0____15
input([prompt])
6
input([prompt])
7
Python is interesting.
The inputted string is: Python is interesting
4
input([prompt])
9

Ví dụ 2: Lấy hai số nguyên từ người dùng và thêm chúng.

# get input from user

inputString = input()

print('The inputted string is:', inputString)
0
input([prompt])
7
What color is rose?: red
Rose is red
3
What color is rose?: red
Rose is red
4
What color is rose?: red
Rose is red
5
What color is rose?: red
Rose is red
6

Output:

Hướng dẫn what are input methods in python? - phương thức nhập liệu trong python là gì?

Trong ví dụ này, chúng tôi sẽ xem xét cách lấy đầu vào số nguyên từ người dùng. Để lấy đầu vào số nguyên, chúng tôi sẽ sử dụng int () cùng với python input ()

# get input from user inputString = input('Enter a string:') print('The inputted string is:', inputString)0input([prompt])5 # get input from user inputString = input('Enter a string:') print('The inputted string is:', inputString)2input([prompt])7input([prompt])6input([prompt])7# get input from user inputString = input('Enter a string:') print('The inputted string is:', inputString)6# get input from user inputString = input('Enter a string:') print('The inputted string is:', inputString)7

# get input from user

inputString = input('Enter a string:')

print('The inputted string is:', inputString)
8
input([prompt])
5
# get input from user

inputString = input('Enter a string:')

print('The inputted string is:', inputString)
2
input([prompt])
7
input([prompt])
6
input([prompt])
7
Enter a string: Python is interesting.
The inputted string is: Python is interesting
4
# get input from user

inputString = input('Enter a string:')

print('The inputted string is:', inputString)
7

Python

# get input from user

inputString = input()

print('The inputted string is:', inputString)
4
input([prompt])
5
input([prompt])
6
input([prompt])
7
# get input from user

inputString = input()

print('The inputted string is:', inputString)
8
input([prompt])
9

Python is interesting.
The inputted string is: Python is interesting
0____15
input([prompt])
6
input([prompt])
7
Python is interesting.
The inputted string is: Python is interesting
4
input([prompt])
9

Ví dụ 2: Lấy hai số nguyên từ người dùng và thêm chúng.

input()7input()8

# get input from user

inputString = input()

print('The inputted string is:', inputString)
0input()0

Output:

Hướng dẫn what are input methods in python? - phương thức nhập liệu trong python là gì?


Các loại đầu vào trong Python là gì?

Có hai chức năng có thể được sử dụng để đọc dữ liệu hoặc đầu vào từ người dùng trong Python: Raw_Input () và Input ().Các kết quả có thể được lưu trữ thành một biến.Raw_Input () - Nó đọc lệnh hoặc lệnh đầu vào và trả về một chuỗi.Input () - Đọc đầu vào và trả về một loại python như danh sách, tuple, int, v.v.raw_input() and input(). The results can be stored into a variable. raw_input() – It reads the input or command and returns a string. input() – Reads the input and returns a python type like list, tuple, int, etc.

Hàm input () cho một ví dụ là gì?

Hàm Python Input () được sử dụng để nhận đầu vào từ người dùng.Nó nhắc nhở cho đầu vào người dùng và đọc một dòng.Sau khi đọc dữ liệu, nó chuyển đổi nó thành một chuỗi và trả về nó.Nó ném một lỗi eoferror nếu EOF được đọc.used to get input from the user. It prompts for the user input and reads a line. After reading data, it converts it into a string and returns that. It throws an error EOFError if EOF is read.

Việc sử dụng hàm input () là gì?

Hàm input () đọc một dòng từ đầu vào (thường là từ người dùng), chuyển đổi dòng thành một chuỗi bằng cách loại bỏ dòng mới và trả về nó.Nếu EOF được đọc, nó sẽ tăng một ngoại lệ Eoferror.reads a line from the input (usually from the user), converts the line into a string by removing the trailing newline, and returns it. If EOF is read, it raises an EOFError exception.