In Python 2, you can use the raw_input() function to read input from the keyboard. This function reads a line of text from the user and returns it as a string.
Here’s an example of how to use raw_input() in Python 2:
name = raw_input('Enter your name: ')
print('Hello, ' + name + '!')
Explanation:
- The
raw_input()function prompts the user to enter a string, in this case, their name. - The entered text is stored in the
namevariable. - The
print()function is used to print a message to the screen, including the value of thenamevariable.
In Python 3, raw_input() has been renamed to input(). The example above would be written as follows in Python 3:
name = input('Enter your name: ')
print('Hello, ' + name + '!')
The input() function in Python 3 behaves the same as raw_input() in Python 2, but returns the input as a string, rather than evaluating it as an expression.