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?
Use str.strip(chars) on str with the quote character '"' as chars to remove quotes from the ends of the string.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With