0

If I do the following in python,

string = raw_input('Enter the value')

it will return

Enter the value

and wait until I enter something into the prompt.

Is there a way to retrieve/collect the input I entered in a variable string?

I would like to use the entered value in the following way :

if dict.has_key('string'):
          print dict[string]

Note: I previously made the error of using raw_string but I meant to say raw_input

0

4 Answers 4

5

there's no raw_string function in python's stdlib. do you mean raw_input?

string = raw_input("Enter the value")     # or just input in python3.0
print(string)
Sign up to request clarification or add additional context in comments.

Comments

4

This is very confusing ... I'm not familiar with a raw_string function in Python, but perhaps it's application-specific?

Reading a line of input in Python can be done like this:

import sys
line = sys.stdin.readline()

Then you have that line, terminating linefeed and all, in the variable line, so you can work with it, e.g. use it has a hash key:

line = line.strip()
if line in dict: print dict[line]

The first line strips away that trailing newline, since you hash keys probably don't have them.

I hope this helps, otherwise please try being a bit clearer in your question.

Comments

3

Is this what you want to do?

string = raw_input('Enter a value: ')
string = string.strip()
if string in dict: print dict[string]

Comments

0

import readline provides a full terminal prompt to the raw_input function. You get control keys and history this way. Otherwise, it's no better than the sys.stdin.readline() option.

import readline

while True:
    value = raw_input('Enter a value: ')
    value = value.strip()
    if value.lower() == 'quit': break

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.