How Python converts string to integer [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
This question is not about usage of int function, but rather how it is done internally.
Because source code is in C I don't understand what is going on there.
Maybe someone can explain how Python convert string "123" to integer 123.
What operations are performed for it?

https://github.com/python/cpython/blob/2d305e1c46abfcd609bf8b2dff8d2065e6af8ab2/Objects/longobject.c#L2075-L2366 contains the implementation you're looking for. While understanding the C is useful, there is a large comment in the middle (starting on line 2132) that explains much of the approach.

When converting a python string to an int, e.g. a = int("123",10), (convert the string "123" to an integer in base 10) a C function is is called.
First, it checks that the given counting base base is >= 2 and <=36, or 0. (Error otherwise)
Next, it ignores all leading spaces. (so that " 123" = "123"),
and check if the number is marked as positive '+', or negative '-'
When the base is 0, it checks if the string starts with '0x','0o', '0b', '0', and sets the base respectively (hexadecimal, octal, binary, decimal).
Note that if no base is given, then the default base is 10 (Decimal).
It then proceeds to turning the character array into a number, using the algorithm described in the code comment at the link posted by Paul Kehrer
Trailing spaces are also ignored, and Errors are raised if needed- for example if there's a space in the middle of the string, followed by a number, or if there's a non-number character.

Related

Replace every caret with a superscript in a python string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I want to replace every caret character with a unicode superscript, for nicer printing of equations in python. My problem is, every caret may be followed by a different exponent value, so in the unicode string u'\u00b*', the * wildcard needs to be the exponent I want to print in the string. I figured some regex would work for this, but my experience with that is very little.
For example, supposed I have a string
"x^3-x^2"
, I would then want this to be converted to the unicode string
u"x\u00b3-x\u00b2"
You can use re.sub and str.translate to catch exponents and change them to unicode superscripts.
import re
def to_superscript(num):
transl = str.maketrans(dict(zip('1234567890', '¹²³⁴⁵⁶⁷⁸⁹⁰')))
return num.translate(transl)
s = 'x^3-x^2'
out = re.sub('\^\s*(\d+)', lambda m: to_superscript(m[1]), s)
print(out)
Output
x³-x²

Use u'string' on string stored as variable in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
As a French user of Python 2.7, I'm trying to properly print strings containing accents such as "é", "è", "à", etc. in the Python console.
I already know the trick of using u before the explicit value of a string, such as :
print(u'Université')
which properly prints the last character.
Now, my question is: how can I do the same for a string that is stored as a variable?
Indeed, I know that I could do the following:
mystring = u'Université'
print(mystring)
but the problem is that the value of mystring is bound to be passed into a SQL query (using psycopg2), and therefore I can't afford to store the u inside the value of mystring.
so how could I do something like
"print the unicode value of mystring" ?
The u sigil is not part of the value, it's just a type indicator. To convert a string into a Unicode string, you need to know the encoding.
unicodestring = mystring.decode('utf-8') # or 'latin-1' or ... whatever
and to print it you typically (in Python 2) need to convert back to whatever the system accepts on the output filehandle:
print(unicodestring.encode('utf-8')) # or 'latin-1' or ... whatever
Python 3 clarifies (though not directly simplifies) the situation by keeping Unicode strings and (what is now called) bytes objects separate.

Split a string by a comma and cast the second half as an int [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
With the following expected input:
[u'able,991', u'about,11', u'burger,15', u'actor,22']
How can I split each string by the comma and return the second half of the string as an int?
This is what I have so far:
def split_fileA(line):
# split the input line in word and count on the comma
<ENTER_CODE_HERE>
# turn the count to an integer
<ENTER_CODE_HERE>
return (word, count)
One of the first things you'll need in learning how to code, is to get to know the set of functions and types you have natively available to you. Python's built-in functions is a good place to start. Also get the habit of consulting the documentation for the stuff you use; it's a good habit. In this case you'll need split and int. Split does pretty much what it says, it splits a given string into multiple tokens, given a separator. You'll find several examples with a simple search in google. int, on the other hand, parses a string (one of the things it does) into a numeric value.
In your case, this is what it means:
def split_fileA(line):
# split the input line in word and count on the comma
word, count = line.split(',')
# turn the count to an integer
count = int(count)
return (word, count)
You won't get this much here in stackoverflow, has other users are often reluctant to do your homework for you. It seems to me that you are at the very beginning of learning how to code so I hope this helps you get started, but remember that learning is also about trial and error.

What is the correct type of this of 's' [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Hi guys going trough some python code, and i came across this line (if statement line).After i check the type of 's' after the statement ,it is still of 'str type'.How does int(s[0])%2==0) affect 's'.Does it slice 's' first, and then convert the obtained value to int type?.the relevant part of the code:
values = []
for i in range(1000, 3001):
s = str(i)
if (int(s[0])%2==0):
#print type(s) #type str
values.append(s)
print ",".join(values)
When you write int(x) it will take whatever the value of x and convert it into an integer. It doesn't change the type of the variable x.
This is called Casting and you can get more information here
If you don't include the casting operation there, it will result in error as you are trying to do modulus operation on a string value, will will be an illegal operation.
So you are taking the value which s[0] holds, converts / casts that into an integer and then perform the Modulus operation.
Your code will take in a number, for example 1782, and will convert it to a string so that you can take the first character from the number. Thus, we will get "1" from the aforementioned number. Then, it converts to an integer so that it can calculate module % on the number, returning 1 if the number is odd, and 0 if it is even. Thus, if num == 0 or in pseudocode if num is even, we proceed.
Effectively, thus code will enter the if statement if the first digit of the number i is even.
If you had not casted int(s[0]) you would not be able to check if the first character in the string s is even (which is what int(s[0])%2==0 is doing). So try it: take off the int and you will get a type error.

What's the best way to convert an integer (0-255) to an escaped byte (\x00 - \xff)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Basically what I'm asking is, what's the most direct way to convert any integer between 0 and 255 into it's hexadecimal, escaped equivalent? One that I mean will function correctly if wrapped in a write() function (which means '\x56' writes 'V' and not literally '\x56'.
That's what the chr function is for.
f.write(chr(0x56))
Speaking of hexadecimal escaped equivalents isn't really relevant in this context - every character has a hexadecimal equivalent, but in expressing a string the characters that can be expressed as a single simple character are simply output as the character.

Categories

Resources