Python 2.7 Getting User Input and Manipulating as String Without Quotations

Python 2.7 getting user input and manipulating as string without quotations

Use raw_input() instead of input():

testVar = raw_input("Ask user for something.")

input() actually evaluates the input as Python code. I suggest to never use it. raw_input() returns the verbatim string entered by the user.

Python string input gives error unless with quotes

In Python 2.7, input() evaluates the input as code, so strings need to be quoted. Python 2.7 has a method called raw_input() that treats all input as strings (no quotes needed).

In Python 3.x, the Python 2.7 raw_input() method was renamed input() and the Python 2.7 input() functionality was replaced by eval(input())

So you can use raw_input() in Python 2.7 or switch to Python 3.x

In python, is there anyway to input a string without quotation marks?

Use raw_input:

table = raw_input("table: ")

>input([prompt])

Equivalent to eval(raw_input(prompt))

This function does not catch user errors. If the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation.

If the readline module was loaded, then input() will use it to provide elaborate line editing and history features.

Consider using the raw_input() function for general input from users.

Python, removing required quotes from input()

On Python 2.x you need to be using raw_input() rather than input(). On the older version of Python, input() actually evaluates what you type as a Python expression, which is why you need the quotes (as you would if you were writing the string in a Python program).

There are many differences between Python 3.x and Python 2.x; this is just one of them. However, you could work around this specific difference with code like this:

try:
input = raw_input
except NameError:
pass

# now input() does the job on either 2.x or 3.x

Python 2.7 taking input from user for two separate variables

the input function (see doc here) will try to evaluate the provided input. For example, if you feed it with your name AMcCauley13 it will look for a variable named so.
Feeding it with values like 1,10 will evaluate in a tuple (1,10), which will break the int() function that expects a string.

Try with the simpler

getMin = int(raw_input("min range: "))
getMax = int(raw_input("max range: "))

or combining split and map as balki suggested in the meanwhile

NameError Using Input - Python 2.7

You need to use raw_input instead of input on python 2.

Using input attempts to evaulate whatever you pass it. In the case of an integer it just resolves to that integer. In the case of a string, it'll attempt to find that variable name and that causes your error.

You can confirm this by typing a 'password' such as password which would have your input call return a reference to your password function.

Conversely, raw_input always returns a string containing the characters your user typed. Judging by your attempt to cast whatever the user types back into a string, this is exactly what you want.

userPassword = raw_input("Type Your Password: ")

I'm writing a code to take a user input and provide its documentation in python. But the string in the user input is used with quotes in python

You can use eval():

user_input = input("\nWhat can I help you with? ")
user_input = user_input.strip('"')
print("\n>>"+eval(user_input).__doc__)

Allow User Input Without Moving to New Line - Python 2.7

raw_input takes an argument. So just do

raw_input('Please enter in the length of the first side: ')

and the user will be prompted to input right after the colon.

Removing brackets and quotes from print in Python 2.7

re.findall returns a list. When you do str(someList), it will print the brackets and commas:

>>> l = ["a", "b", "c"]
>>> print str(l)
['a', 'b', 'c']

If you want to print without [ and ,, use join:

>>> print ' '.join(l)
a b c

If you want to print without [ but with ,:

>>> print ', '.join(l)
a, b, c

And if you want to keep the ', you could use repr and list comprehension:

>>> print ', '.join(repr(i) for i in l)
'a', 'b', 'c'

After your edit, it seems that there is only 1 element in your lists. So you can only print the first element:

>>> l = ['a']
>>> print l
['a']
>>> print l[0]
a


Related Topics



Leave a reply



Submit