Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

It is maybe a silly question but I'm trying to translate a javascript sentence to python and I can't find a way to convert an integer to string like javascript.

Here the javascript sentence:

var n = 11;
n = n.toString(16);

It returns 'b'.

I tried chr() in python but it is not the same. I don't know to program in javascript so I would be grateful if someone can help me to understand how does javascript convertion works to do that.

Thanks you.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

the line

n = n.toString(16);

Is converting the number 11 to a string base 16 or 0xB = 11 decimal.

you can read more about int.toString

the code you want is:

n = 11
n = format(n, 'x')

or

n = hex(n).lstrip('0x')

the lstrip will remove the 0x that is placed when converting to hex

share|improve this answer
    
Thank you I found more information here tools.ietf.org/html/rfc3548.html. –  Ricardo Apr 22 '14 at 22:06
    
Another approach is: n = '%x' % n –  William McBrine Apr 23 '14 at 1:36

Everything below was my original answer, didn't see it was base 16 instead of base 10. This is not the solution.

I wonder how much effort you took in searching for an answer: Converting integer to string in Python? was the first result when googling "python integer to string". Taking a look at the search result, this should do it:

n = 11
n = str(n)

This might work too:

n = 11
n.__str__()

You can give it a try here http://progzoo.net/wiki/Python:Convert_a_Number_to_a_String

share|improve this answer
    
What I want is to get the same result that when I use toString() in javascript not to keep the n value as 11 –  Ricardo Apr 22 '14 at 21:52
    
Didn't see the the "16" and thought that the result "b" was an unwanted value from python code. Sorry for implicitly accusing you of being lazy. –  TiLor Apr 22 '14 at 23:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.