I want to get a string which merely consists of 0 and 1.
Since raw_input() was renamed to input() in Python 3, I wrote:
buf = str(input())
print(buf)
However,when I tried '00101', buf surprisingly turned out to be '65'.
Why this happened and how to prevent this?
As a beginner in Python, it confuses me so much.
update: Problem solved. See my answer below.
i try this statement in python3 and output was 00101
btw: output from function input() is string, you dont need call str() on input()
but if you run input() in python (python 2.7) you get output 65
Problem solved. I found that I forgot to remove Python 2.7 directory from environment variables. Furthermore, a number begins with zero is considered as an octal one. That's why I got '65' for '00101' in Python 2. Thank you all.
Related
In Python3 the behaviour of the inbuilt repr() function has changed when compared to Python2.
In python2
repr("Дует Світязь")
"'\\xd0\\x94\\xd1\\x83\\xd0\\xb5\\xd1\\x82 \\xd0\\xa1\\xd0\\xb2\\xd1\\x96\\xd1\\x82\\xd1\\x8f\\xd0\\xb7\\xd1\\x8c'"
In Python3
repr("Дует Світязь")
"'Дует Світязь'"
How can I get repr() to work in Python3 the same as in Python2 or as an alternative another method to convert a string to escaped characters
In my code. I use the escaped characters in lookup tables for my LCD display translation routines.
The platform is Raspberry Pi OS(Raspbian).
I have also tried using reprlib() without success. It gives the same output as repr(). Using string.encode goes some way to proving a solution, but the result is different to Python2 repr()
txt = "Дует Світязь"
txt.encode('utf8')
b'\xd0\x94\xd1\x83\xd0\xb5\xd1\x82 \xd0\xa1\xd0\xb2\xd1\x96\xd1\x82\xd1\x8f\xd0\xb7\xd1\x8c'
You can encode the string as bytes object:
print('Дует Світязь'.encode('utf-8'))
Prints:
b'\xd0\x94\xd1\x83\xd0\xb5\xd1\x82 \xd0\xa1\xd0\xb2\xd1\x96\xd1\x82\xd1\x8f\xd0\xb7\xd1\x8c'
EDIT:
s = str('Дует Світязь'.encode('utf-8')).lstrip('b').replace('\\', r'\\')
print(s)
Prints:
'\\xd0\\x94\\xd1\\x83\\xd0\\xb5\\xd1\\x82 \\xd0\\xa1\\xd0\\xb2\\xd1\\x96\\xd1\\x82\\xd1\\x8f\\xd0\\xb7\\xd1\\x8c'
My thanks to Andrej Kesely who put me on the right path for a solution
In Python2
s = repr(text)
In Python3
s = str(text.encode('utf-8')).lstrip('b')
I don't really understand why the authors of Python3 felt the need to change how a built in function such as repr() works. No doubt they have their reasons.
I have been trying to build a randomized greeting program, so far I got:
import random
a = ("Hi!", "How are you?", "You Good?")
b = random.choice(a)
print(b)
It used to work, but now it just says that there are no attributes for .choice, can someone help?
Edit1: I think my Python is broken...
Edit2: Wait it's fixed now...
Rename your own script to something different than random.py.
Your code is correct. Here are some step to refine or rebuild you code:
Rename the file and try again.
In random.choice(a). 'a' should be a List, Tuple, or String only.
try with square brackets [] instead of small bracket (). Like a = ["Hi!", "How are you?", "You Good?"]
Update your Python with the latest version.
What is the good way to get string output with u prefix in python3?
I'd like to use a library to do this like six.
I'm fixing assert testing with string output under compatible with python2.
import six
assert(repr(six.text_type('TEST TEXT')) == "u'TEST TEXT'")
For python 3, strings are unicode by default, so you won't see the u prefix.
For testing purposes, you should simply compare the values, not the representation.
expected = u'TEST TEXT'
assert(actual == expected)
In Python3, you can use String formatting to set the 'u' prefix. Please see the example below:
print('u{}'.format("TEST TEXT"))
Output will be: uTEST TEXT
Please note that this will only work in Python 3 and above.
Thanks!
I'm trying to code in Python 3. So far I've used codeacademy to copy and paste the functions I've wanted. Unless what codeacademy uses is python 2 (which it's not, I've checked). So I'm curious why it highlights len and says invalid syntax.
print ('Have you thought today?')
original = raw_input('Yes or No:')
If len(original) > 2:
print ('When?')
You put If capitalized. Python is case sensitive so you must use the correct keyword which is lowercase if.
Also raw_input() was renamed to input() in Python 3.
python3 doesn't have raw_input().
Change
original = raw_input('Yes or No:')
to:
original = input('Yes or No:')
And, also use if in place of If.
I'm having trouble with the input() function in Python3.4 using the Anaconda integrated editor. If I just type
x = input()
into the editor, it returns a blank line that I can type text into. If I type:
foo
into this line, I would expect 'foo' be stored as a string with variable name x. But, instead I get:
NameError: name 'foo' is not defined
To make the function work as expected, I must instead type in:
'foo'
which is unfortunate because what I really want is just to pause my code and wait for an arbitrary user input, and I read somewhere that "wait = input()" is the most pythonic way to do this. Using that line in my actual script returns an "unexpected EOF" error - I assume as another symptom of the same problem. Can anyone suggest a workaround?
Note: I suspect this is an Anaconda-specific problem, given the following reference:
https://docs.python.org/3.4/library/functions.html#input
Thanks for your time.
Your code is being run by Python 2, not 3. I don't know enough about Anaconda to know if the problem is with their editor, or if you have your path messed up, but the problem is that the wrong version of Python is being used.