How to read string from terminal in Python

Reading a character or string from the terminal is a basic operation in most languages. It is most commonly used for a quick debug or analysis of the program.

To do this in Python 2.x:

print "Enter something:"
s = raw_input()
print "You entered:", s

To do this in Python 3.x:

print("Enter something:")
s = input()
print("You entered:", s)

Note that in Python 2.x, the built-in function is raw_input, while it has been renamed to input in Python 3.x.

Tried with: Ubuntu 14.04

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s