1

I am trying to iterate over the input011 as a string like:

val = str(011)for _iter in range(len(val)):    if _iter[i]=='1':        print "yes"

But on checking the values, it seems to give different ouput.

>>> val = str(011)>>> val'9'>>> val = str(42565)>>> val'42565'

Why I am getting'9' for the above value ??

Implementation:

I want to display the values of a list
supposelist = [1,2,3] according to the string011
so the output will be

23
askedAug 12, 2016 at 12:43
bhansa's user avatar
2
  • 1
    Can you elaborate on the output you're trying to get?CommentedAug 12, 2016 at 12:44
  • 2
    Isn't the0 prefix for octal? Also, use0b for binary and0o for octal and forward compatibility.CommentedAug 12, 2016 at 12:47

1 Answer1

2
011

Having a '0' prefix, is interpreted as anoctal (base 8) number. 1*8 + 1 = 9

If you want to iterate over the characters, then simply make it a string by enclosing it in quotes:

val = '011'

To convert a string of 0 and 1 characters like this to aninteger, you can callint like this:

int(val, base=2)

To bypass the string parsing, and simply assign a binary constant to a variable, use the0b prefix:

val = 0b011print val     # outputs 3
answeredAug 12, 2016 at 12:46
Jonathon Reinhart's user avatar
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.