1

OK, this is completely confusing me to bits, I am printing a string and its returning numbers. I don't understand how it is returning numbers from a string.

Here is the code snippet.

string = "String"
print int(string[0:min(5,len(string))],36)

the output of that snippet is

48417935

My friend was telling me it has something to do with the computer generating numbers from strings, but I am just so confused.

Can someone please be kind enough and explain to why this is happening?

Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97

6 Answers6

5

You are taking this slice

>>> string[0:min(5,len(string))]
'Strin'

and converting it as a base36 number (similar to hexadecimal but using all 26 letters)

>>> int('Strin', 36)
48417935

Another way to arrive at this figure is:

>>> ["0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".index(x) for x in 'STRIN']
[28, 29, 27, 18, 23]
>>> 28*36**4 + 29*36**3 + 27*36**2 + 18*36**1 + 23*36**0
48417935
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
0

You are printing the return value of the int method, which returns a number, not a string.

asherbret
  • 5,439
  • 4
  • 38
  • 58
0

You are using the int built-in function to convert the string to integer.

from, Python docs

class int(x=0) class int(x, base=10)

Return an integer object constructed from a number or string x, or return 0 if no arguments are given. If x is a number, it can be a plain integer, a long integer, or a floating point number. If x is floating point, the conversion truncates towards zero. If the argument is outside the integer range, the function returns a long object instead.

If x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in radix base. Optionally, the literal can be preceded by + or - (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O/0, or 0x/0X, as with integer literals in code. Base 0 means to interpret the string exactly as an integer literal, so that the actual base is 2, 8, 10, or 16.

If you break down your code in small snippets, it will be more readable and clear to you::

string = "String"
len_of_str = len(string) # get length of "string" i.e., 6
var_a_minimum = min(5, len_of_str) # get the minimum of 5 or len_of_str
var_x_string = string[0:var_a_minimum ] # slice the string from 0 to var_a_minimum 
var_y_integer = int(var_x_string,36) # get the integer value of var_x_string to the base-36
print var_y_integer 
Abhineet
  • 5,320
  • 1
  • 25
  • 43
0

Your first line of code creates a variable string

string = "String"

Lets make your print statement more understandable.

length=len(string)   #finds the length of text in string variable in this case 6

then you are doing min(5,length). The expression finds the smallest integer between 5 and 6. So it evaluates to 5.

and finally string[0:5] this slices the string and gets all the characters from 0 to 4.So it evaluates to "Strin" in your example Read about slicing here

then finally you are doing print int("Strin",36) which will return a base36 integer. Read about int() here

Community
  • 1
  • 1
m0bi5
  • 8,900
  • 7
  • 33
  • 44
0

You should always try whatever confuses you in python shell piece by piece, that way you can understand what is happening. Use a better shell like ipython and be liberal to use its ? to see what's happening in the background. Python is kind of a "self documenting" language.

Let's go through your code piece by piece :

print int(string[0:min(5,len(string))],36)

Ok let's start with min(5,len(string))

In [2]: string = "String"

In [3]: min(5,len(string))
Out[3]: 5

In [4]: min?
Docstring:
min(iterable[, key=func]) -> value
min(a, b, c, ...[, key=func]) -> value

With a single iterable argument, return its smallest item.
With two or more arguments, return the smallest argument.
Type:      builtin_function_or_method

Pretty self explanatory.

Well let's go one step ahead :

string[0:min(5,len(string))]

We have already got a value out of min() call , so this boils down do :

string[0:5]

As we already from the python's way of list slicing, it will return the 5 elements of the string starting from string[0] and ending with string[4] .

So in our given string's case it will return :

In [5]: string[0:min(5,len(string))]
Out[5]: 'Strin'

now what does int('Strin',36) means?

Let's go back to the shell :

In [6]: int??
Docstring:
int(x=0) -> int or long
int(x, base=10) -> int or long

Convert a number or string to an integer, or return 0 if no arguments
are given.  If x is floating point, the conversion truncates towards zero.
If x is outside the integer range, the function returns a long instead.

So it's converting it to number in a 36 based number system. Lets see the default invocation for one last time...

In [7]: int('Strin')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-a13c6c79aa49> in <module>()
----> 1 int('Strin')

ValueError: invalid literal for int() with base 10: 'Strin'

Well that's expected, as base 10 number system doesn't have symbols S or T etc. Base 16 has extra symbols A to F. so it means base 36 system would have 36-10=26 symbols. That means it will have all of the english alphabet as it's symbol table. That's why it will not raise exception and will be able to convert any string literal to a number representation.

Aftnix
  • 4,461
  • 6
  • 26
  • 43
0

The product of string[0:min(5,len(string))] is Strin. You then us int() on it, which means you try to make a string an int. As the documentation for int() states,

f x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in radix base. Optionally, the literal can be preceded by + or - (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35.

An integer literal is defined by wikipedia as:

an integer literal is an integer whose value is directly represented in source code. For example, in the assignment statement x = 1, the string 1 is an integer literal indicating the value 1, while in the statement x = 0x10 the string 0x10 is an integer literal indicating the value 16, which is represented by 10 in hexadecimal (indicated by the 0x prefix).

By contrast, in x = cos(0), the expression cos(0) evaluates to 1 (as the cosine of 0), but the value 1 is not literally included in the source code. More simply, in x = 2 + 2, the expression 2 + 2 evaluates to 4, but the value 4 is not literally included. Further, in x = "1" the "1" is a string literal, not an integer literal, because it is in quotes. The value of the string is 1, which happens to be an integer string, but this is semantic analysis of the string literal – at the syntactic level "1" is simply a string, no different from "foo".

So the interpreter is taking in 'Strin' and computing it as a number using base 36. You can play around with this and see that you need at least base 30 to not throw an error, because 0 - 9 and a - t are 30 characters in total.

Community
  • 1
  • 1
dkhamrick
  • 402
  • 2
  • 8