Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, removing required quotes from input() [closed]

Tags:

python

Ok, I've been putting together a script but there's an annoying thing that happens with the input part. Depending on which version of python I have, I either need to include quotes for my input or I don't. With python 2.7, I need quotes; with python 3.3, I don't. For example:

filename = input('Enter Update File: ')
print(filename)

With python 2.7, I need to surround my input with quotes or it raises a NameError; in python 3.3, I don't.

Is there a way to avoid this?

like image 624
rickman90 Avatar asked Apr 23 '13 17:04

rickman90


People also ask

How do you remove quotes from a input in Python?

Use str.strip(chars) on str with the quote character '"' as chars to remove quotes from the ends of the string.

How do you print without quotes in Python?

strip() If you want to remove the enclosing quotes from a string before printing it, you can call the string. strip() method and pass the single and double quotes characters to be stripped from the beginning and end of the string object on which it is called.

How do you remove quotes from a string?

In addition to the substring method, we can also use the replaceAll method. This method replaces all parts of the String that match a given regular expression. Using replaceAll, we can remove all occurrences of double quotes by replacing them with empty strings: String result = input.


1 Answers

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
like image 102
kindall Avatar answered Nov 13 '22 10:11

kindall