Surprisingly, people were giving only solutions that convert to small bases (smaller than the length of the English alphabet). There was no attempt to give a solution which converts to any arbitrary base from 2 to infinity.

So here is a super simple solution:

def numberToBase(n, b):
    if n == 0:
        return [0]
    digits = []
    while n:
        digits.append(int(n % b))
        n //= b
    return digits[::-1]

so if you need to convert some super huge number to the base 577,

numberToBase(67854 ** 15 - 102, 577), will give you a correct solution: [4, 473, 131, 96, 431, 285, 524, 486, 28, 23, 16, 82, 292, 538, 149, 25, 41, 483, 100, 517, 131, 28, 0, 435, 197, 264, 455],

Which you can later convert to any base you want

  1. at some point of time you will notice that sometimes there is no built-in library function to do things that you want, so you need to write your own. If you disagree, post you own solution with a built-in function which can convert a base 10 number to base 577.
  2. this is due to lack of understanding what a number in some base means.
  3. I encourage you to think for a little bit why base in your method works only for n <= 36. Once you are done, it will be obvious why my function returns a list and has the signature it has.
Answer from Salvador Dali on Stack Overflow
๐ŸŒ
SheCodes
shecodes.io โ€บ athena โ€บ 2142-converting-an-integer-to-string-in-python
[Python] - Converting an Integer to String in Python - | SheCodes
Learn how to convert an integer to a string in Python by using the `str()` function or casting the integer as a string.
Discussions

python - How to convert an integer to a string in any base? - Stack Overflow
Python allows easy creation of an integer from a string of a given base via int(str, base). I want to perform the inverse: creation of a string from an integer, i.e. I want some function int2ba... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Convert integer to string in Python - Stack Overflow
How do I convert an integer to a string? 42 โŸถ "42" For the reverse, see How do I parse a string to a float or int?. Floats can be handled similarly, but handling the decimal points ca... More on stackoverflow.com
๐ŸŒ stackoverflow.com
i am a beginner at python and im trying to convert string to int but it doesn't work
What? You can't convert "sadsad" to an int. What would that even mean? Don't you mean you want to convert the integer 170 to a string, instead? More on reddit.com
๐ŸŒ r/learnpython
21
1
August 8, 2023
Quick question: Is there a way to get the first three digits of a int without converting it to a string or list?

You probably shouldn't be keeping phone numbers as ints, because they don't behave like ints. You'll never add, subtract, multiply, or divide phone numbers. You will, however, access individual digits of them, so an array is a much more appropriate type. If you absolutely must store them as ints for efficiency reasons (unlikely), then at point of use you should probably just convert them, unless that is too expensive (even more unlikely).

Remember readability is generally more important than minor performance gains, so digits(num)[:3] is much easier to understand than (num-num%le6)/le6 as skier_scott suggests doing.

More on reddit.com
๐ŸŒ r/Python
36
16
February 13, 2011
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ string-to-integer-atoi
String to Integer (atoi) - LeetCode
String to Integer (atoi) - Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer. The algorithm for myAtoi(string s) is as follows: 1. Whitespace: Ignore any leading whitespace (" "). 2. Signedness: Determine ...
๐ŸŒ
Flexiple
flexiple.com โ€บ python โ€บ convert-string-to-int
Python Convert String to Int - How to Cast a String in Python - Flexiple
February 21, 2024 - Here, the float() function converts the string "123.45" to a floating-point number, which is then converted to an integer 123 using the int() function, truncating the decimal part.
Top answer
1 of 16
249

Surprisingly, people were giving only solutions that convert to small bases (smaller than the length of the English alphabet). There was no attempt to give a solution which converts to any arbitrary base from 2 to infinity.

So here is a super simple solution:

def numberToBase(n, b):
    if n == 0:
        return [0]
    digits = []
    while n:
        digits.append(int(n % b))
        n //= b
    return digits[::-1]

so if you need to convert some super huge number to the base 577,

numberToBase(67854 ** 15 - 102, 577), will give you a correct solution: [4, 473, 131, 96, 431, 285, 524, 486, 28, 23, 16, 82, 292, 538, 149, 25, 41, 483, 100, 517, 131, 28, 0, 435, 197, 264, 455],

Which you can later convert to any base you want

  1. at some point of time you will notice that sometimes there is no built-in library function to do things that you want, so you need to write your own. If you disagree, post you own solution with a built-in function which can convert a base 10 number to base 577.
  2. this is due to lack of understanding what a number in some base means.
  3. I encourage you to think for a little bit why base in your method works only for n <= 36. Once you are done, it will be obvious why my function returns a list and has the signature it has.
2 of 16
128

If you need compatibility with ancient versions of Python, you can either use gmpy (which does include a fast, completely general int-to-string conversion function, and can be built for such ancient versions โ€“ you may need to try older releases since the recent ones have not been tested for venerable Python and GMP releases, only somewhat recent ones), or, for less speed but more convenience, use Python code โ€“ e.g., for Python 2, most simply:

import string
digs = string.digits + string.ascii_letters


def int2base(x, base):
    if x < 0:
        sign = -1
    elif x == 0:
        return digs[0]
    else:
        sign = 1

    x *= sign
    digits = []

    while x:
        digits.append(digs[int(x % base)])
        x = int(x / base)

    if sign < 0:
        digits.append('-')

    digits.reverse()

    return ''.join(digits)

For Python 3, int(x / base) leads to incorrect results, and must be changed to x // base:

import string
digs = string.digits + string.ascii_letters


def int2base(x, base):
    if x < 0:
        sign = -1
    elif x == 0:
        return digs[0]
    else:
        sign = 1

    x *= sign
    digits = []

    while x:
        digits.append(digs[x % base])
        x = x // base

    if sign < 0:
        digits.append('-')

    digits.reverse()

    return ''.join(digits)
๐ŸŒ
Domyassignments
domyassignments.com โ€บ home โ€บ converting string to integer in python: a comprehensive guide
Converting String to Integer in Python: A Comprehensive Guide
June 28, 2024 - The most popular way to convert string to integer in Python is to use the int() function. This function is a fundamental built-in Python function, which is utilized to convert a given value into an integer.
Find elsewhere
๐ŸŒ
Sabe
sabe.io โ€บ blog โ€บ python-convert-string-to-integer
How to Convert a String to Integer in Python | Sabe
June 19, 2022 - The most straightforward way to convert a string to an integer is to use the int() function. This is a built-in function in Python that takes in your string and attempts to convert it to an integer for you.
๐ŸŒ
Python Geeks
pythongeeks.org โ€บ python geeks โ€บ learn python โ€บ convert string to int in python
Convert String to int in Python - Python Geeks
December 31, 2025 - Converting a string to an integer is a common operation in Python, and it can be done easily using the int() function. In this article, we learned what the int() function is and how it works.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_casting.asp
Python Casting
Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types. Casting in python is therefore done using constructor functions: int() - constructs an integer number from an integer literal, a float literal (by removing all decimals), or a string literal (providing the string represents a whole number)
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ string.html
string โ€” Common string operations
The '#' option causes the โ€œalternate formโ€ to be used for the conversion. The alternate form is defined differently for different types. This option is only valid for integer, float and complex types. For integers, when binary, octal, or hexadecimal output is used, this option adds the respective prefix '0b', '0o', '0x', or '0X' to the output value.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ functions.html
Built-in Functions โ€” Python 3.14.3 documentation
1 week ago - As repr(), return a string containing a printable representation of an object, but escape the non-ASCII characters in the string returned by repr() using \x, \u, or \U escapes. This generates a string similar to that returned by repr() in Python 2. ... Convert an integer number to a binary ...
๐ŸŒ
Linuxize
linuxize.com โ€บ home โ€บ python โ€บ how to convert integer into string in python
How to Convert Integer into String in Python | Linuxize
November 25, 2020 - Letโ€™s try to concatenate strings and integers using the + operator and print the result: ... number = 6 lang = "Python" quote = "There are " + number + " relational operators in " + lang + "." print(quote)
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-string-to-int-int-to-string
Python String to Int, Int to String | DigitalOcean
August 4, 2022 - If the string you want to convert into int belongs to different number base other that base 10, you can specify the base for conversion. But remember that the output integer is always in base 10. Another thing you need to remember is that the given base must be in between 2 to 36.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-convert-integers-to-strings-in-python-3
How To Convert Integers to Strings in Python 3 | DigitalOcean
September 3, 2020 - Weโ€™re not able to concatenate strings and integers in Python, so weโ€™ll have to convert the variable lines to be a string value:
๐ŸŒ
Kanaries
docs.kanaries.net โ€บ topics โ€บ Python โ€บ covnert-string-to-int-python
How to Convert String to Int in Python: Easy Guide โ€“ Kanaries
Python offers several built-in functions for converting between different data types: float(): Converts a number or a string to a float. ... When converting strings to integers in Python, it's essential to handle potential errors gracefully. One approach is to use a try-except block, which captures exceptions and allows you to define custom behavior in case of a conversion error.