Hướng dẫn which function takes input in python? - hàm nào nhận đầu vào trong python?

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

Xem thảo luận

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

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

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

    Lưu bài viết

    Đọc is used to take user input. By default, it returns the user input in form of a string.

    Bàn luận

    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. input(prompt)

    • Python Input () Chức năng cú phápany string value to display as input message

    Cú pháp: Đầu vào (Nhắc) Return a string value as input by the user.

    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ào

    Python3

    Trả về: Trả về giá trị chuỗi làm đầu vào của người dùng.

    input([prompt])
    9
    input([prompt])
    6
    # get input from user
    
    

    inputString = input()

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

    inputString = input()

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

    Output:

    What color is rose?: red
    Rose is red

    Ví dụ về hàm Python Input ()the Name and Age of the user as input and printing it

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

    Python

    # get input from user
    
    

    inputString = input()

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

    inputString = input()

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

    # get input from user
    
    

    inputString = input()

    print('The inputted string is:', inputString)
    9
    input([prompt])
    4
    input([prompt])
    5
    input([prompt])
    6
    Python is interesting.
    The inputted string is: Python is interesting
    3
    input([prompt])
    8

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

    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

    Python is interesting.
    The inputted string is: Python is interesting
    9
    input([prompt])
    4
    # get input from user
    
    

    inputString = input('Enter a string:')

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

    inputString = input('Enter a string:')

    print('The inputted string is:', inputString)
    5
    # 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
    input([prompt])
    4
    # get input from user
    
    

    inputString = input('Enter a string:')

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

    inputString = input('Enter a string:')

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

    Enter a string: Python is interesting.
    The inputted string is: Python is interesting
    5
    input([prompt])
    4
    Python is interesting.
    The inputted string is: Python is interesting
    9
    Enter a string: Python is interesting.
    The inputted string is: Python is interesting
    8
    Enter a string: Python is interesting.
    The inputted string is: Python is interesting
    9

    input([prompt])
    9
    input([prompt])
    6
    What color is rose?: red
    Rose is red
    2
    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

    Output:

    Hướng dẫn which function takes input in python? - hàm nào nhận đầu vào trong python?

    Tương tự, chúng ta có thể sử dụng float () để lấy hai số float. Hãy để xem thêm một ví dụ về cách lấy danh sách làm đầu vào

    Ví dụ 3: Lấy hai danh sách làm đầu vào và thêm chúng

    Lấy đầu vào của người dùng làm chuỗi và phân tách trên mỗi ký tự bằng danh sách () để chuyển đổi thành danh sách các ký tự.

    Python

    What color is rose?: red
    Rose is red
    6
    input([prompt])
    4
    What color is rose?: red
    Rose is red
    8
    input([prompt])
    6
    input([prompt])
    5
    input([prompt])
    6
    Please Enter Your Name: Rohit
    Please Enter Your Age: 16
    Name & Age:  Rohit 16 
    2
    # get input from user
    
    

    inputString = input('Enter a string:')

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

    Please Enter Your Name: Rohit
    Please Enter Your Age: 16
    Name & Age:  Rohit 16 
    4
    input([prompt])
    4
    What color is rose?: red
    Rose is red
    8
    input([prompt])
    6
    input([prompt])
    5
    input([prompt])
    6input()0
    # get input from user
    
    

    inputString = input('Enter a string:')

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

    input()2 input()3input()4 input()5

    input()6input()7

    input([prompt])
    9input()9

    Output:

    Hướng dẫn which function takes input in python? - hàm nào nhận đầu vào trong python?


    Chức năng nào sử dụng đầu vào trong Python?

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

    Làm thế nào để bạn thực hiện một chức năng lấy đầu vào trong Python?

    Sau đó, hãy thử mã sau trong IDE.# Nhận tên của người dùng user_name = input ("Nhập tên của bạn:-") in ("Hi,", user_name, "\ b!") Chúng tôi đã gửi một thông báo cho người dùng nhập tên của bạn:-.Người dùng sẽ đọc tin nhắn và nhập dữ liệu phù hợp.user_name = input("Enter your name:- ") print("Hi,", user_name, "\b!") We have given a message to the user that Enter your name:-. User will read the message and enter the data accordingly.

    Chức năng nào được sử dụng cho đầu vào và đầu ra trong Python?

    Trong Python, chúng tôi sử dụng hàm print () để xuất dữ liệu vào màn hình.Đôi khi chúng tôi có thể muốn lấy đầu vào từ người dùng.Chúng ta có thể làm như vậy bằng cách sử dụng hàm input ().Python lấy tất cả các đầu vào làm đầu vào chuỗi theo mặc định.print() function to output data to the screen. Sometimes we might want to take the input from the user. We can do so by using the input() function. Python takes all the input as a string input by default.