In python 3.6, the fstring or "formatted string literal" mechanism was introduced.

f"{a:02}"

is the equivalent of the .format format below, but a little bit more terse.


python 3 before 3.6 prefers a somewhat more verbose formatting system:

"{0:0=2d}".format(a)

You can take shortcuts here, the above is probably the most verbose variant. The full documentation is available here: http://docs.python.org/3/library/string.html#string-formatting


print "%02d"%a is the python 2 variant

The relevant doc link for python2 is: http://docs.python.org/2/library/string.html#format-specification-mini-language

Answer from jkerian on Stack Overflow
🌐
Sololearn
sololearn.com › en › Discuss › 2576706 › converting-a-double-digit-int-to-a-str-python
Converting a double digit int to a str(Python) | Sololearn: Learn to code for FREE!
Ben Broz , the reason why list.extend("your_string") is separating the digits of the string-representation of an int: extend() expects one argument, which has to be an ▶️ iterable ◀️. This is the case for strings. I suppose you converted the number to string, because by passing an int to the function, it will create an error. You can use append as suggested, or you can use an expression like this: list_one = [2,9,0] number = 10 list_one.extend([number]) print(list_one) #Output: [2, 9, 0, 10]
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › convert-integer-to-string-in-python
Convert integer to string in Python - GeeksforGeeks
Python provides built-in type conversion functions to easily transform one data type into another. This article explores the process of converting objects into strings which is a basic aspect of Python programming.Since every element in Python is an object, we can use the built-in str() and repr() m
Published   April 25, 2025
🌐
Real Python
realpython.com › convert-python-string-to-int
How to Convert a Python String to int – Real Python
January 16, 2021 - There are several ways to represent integers in Python. In this quick and practical tutorial, you'll learn how you can store integers using int and str as well as how you can convert a Python string to an int and vice versa.
🌐
Python Principles
pythonprinciples.com › blog › converting-integer-to-string-in-python
Converting integer to string in Python – Python Principles
To convert an integer to a string, use the str() built-in function. The function takes an int as input and produces a string as its output. Here are some examples.
🌐
Scaler
scaler.com › home › topics › how to convert int to string in python
How to Convert int to string in Python - Scaler Topics
May 12, 2024 - The syntax for the conversions ... the parenthesis. ... The syntax for using the %s keyword is enclosing %s in quotation marks and then writing % integer after it....
🌐
Finxter
blog.finxter.com › home › learn python blog › python int to string with leading zeros
Python Int to String with Leading Zeros - Be on the Right Side of Change
September 27, 2023 - Many Python coders don’t quite get the f-strings and the zfill() method shown in Methods 2 and 3. If you don’t have time learning them, you can also use a more standard way based on string concatenation and list comprehension. # Method 3: List Comprehension s3 = str(i) n = len(s3) s3 = '0' * (5-len(s3)) + s3 print(s3) You first convert the integer to a basic string.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › help with turning the output time into double digits instead of single digit
r/learnpython on Reddit: Help with turning the output time into double digits instead of single digit
August 14, 2022 -

I wrote this code:

SecToConvert = 234
MinutesGet, SecondsGet = divmod(SecToConvert, 60) 
HoursGet, MinutesGet = divmod(MinutesGet,60)
print("Time is:", HoursGet, ":", MinutesGet, ":", SecondsGet)

And it gives this output currently:

Time is: 0 : 3 : 54

And I need it to produce "00: 03: 54" so that it's double digits. I'm not sure how to do this currently, would anyone be able to help me to correct the code in order to get the double-digit time? Thank you!

🌐
Runestone Academy
runestone.academy › ns › books › published › pythonds › Recursion › pythondsConvertinganIntegertoaStringinAnyBase.html
5.5. Converting an Integer to a String in Any Base — Problem Solving with Algorithms and Data Structures
While there are many algorithms ... and the number 769. Suppose we have a sequence of characters corresponding to the first 10 digits, like convString = "0123456789"....
🌐
Unstop
unstop.com › home › blog › convert int to string in python (6 methods with examples)
Convert Int To String In Python (6 Methods With Examples)
April 11, 2024 - The most common way to convert int to string in Python is the inbuilt str() function, which can convert any data type into a string. Learn other methods inside.
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)
🌐
Replit
replit.com › home › discover › how to convert an int to a string in python
How to convert an int to a string in Python
It avoids the long, unformatted decimal tails that often result from floating-point math, ensuring your output is always professional. A common task is converting user input from a string to an integer using the int() function.
🌐
GeeksforGeeks
geeksforgeeks.org › convert-string-to-integer-in-python
Convert String to Int in Python - GeeksforGeeks
Here, int(s, 2) interprets s as a binary string, returning 10 in decimal.
Published   October 27, 2024
🌐
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.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Format Strings and Numbers in Python: format() | note.nkmk.me
May 18, 2023 - Underscores are inserted every ... to binary, octal, or hexadecimal. If you want to format a numeric string, use int() to convert it to an integer....
🌐
Invent with Python
inventwithpython.com › pythongently › exercise32
Exercise 32 - Convert Strings To Integers
To complement Exercise #31, “Convert Integers to Strings”, in this exercise we’ll convert strings of numeric digits into their integer equivalents. The most common use case for this is taking the string returned from, say, the input() function or a text file’s read() method and converting it to an integer to perform mathematical operations on it. You can use Python’s int() function to do this conversion, but in this exercise, you’ll recreate this function yourself.
🌐
Quora
quora.com › How-do-you-convert-int-to-string-without-using-an-in-built-function-in-Python-3-Python-Python-3-x-development
How to convert int to string without using an in-built function in Python 3 (Python, Python 3.x, development) - Quora
Answer (1 of 4): The simple answer is you cannot convert an int to a string without using any builtin functions. You can dress it up so the function isn’t visible but there are still functions being called.
🌐
AskPython
askpython.com › home › how to format a number to 2 decimal places in python?
How to Format a Number to 2 Decimal Places in Python? - AskPython
February 27, 2023 - When working with float values containing a decimal, we sometimes want to have only one or two digits or digits as per requirements after the decimal point. So the question is how to do that? It is pretty simple by using %f formatter or str.format() ...