How do you convert int to string without using library functions in python?

def int_to_str(num):
    is_negative = False
    if num:
        num, is_negative = -num, True
    s = []
    while True:
        s.append(chr(ord('0') + num % 10))
        num //= 10
        if num == 0:
            break
    return ('-' if is_negative else '') + ''.join(reversed(s))
num = input("Enter any numeric value: ")
print(int_to_str(num))    

ayhan

66.1k17 gold badges173 silver badges191 bronze badges

asked Jun 11, 2017 at 21:39

6

If you have only an integer in the conditional of an if statement, the if statement will run if and only if the integer is not equal to zero. You need to do

if num < 0:

not

if num:

But indeed, @user8145959 has a point. The number inputted is already a string. When you pass the input string to int_to_str, it gets automatically converted to an integer at the places where you try integer operations on it.

Edit: Automatic integer conversion works only in python 2, not python 3. My mistake.

Edit2: Actually I'm just wrong. Didn't realize input() did the conversion already.

answered Jun 11, 2017 at 21:47

xunataixunatai

1711 silver badge6 bronze badges

4

The idea is to use the ASCII value of the digits from 0 to 9 start from 48 – 57. 
num = 56

s = [0 if not num else '']
while num:
    s.append(chr(48 + num % 10))
    num = num // 10
a= "".join(reversed(s))

print(a)
print(type(a))

output->
56


answered Dec 10, 2020 at 9:38

NIMINIMI

738 bronze badges

You can use the chr() and ord() just like you did:

def int_to_str(input_int):
    if input_int < 0:
        is_negative = True
        input_int *= -1
    else:
        is_negative = False

    output_str = []
    while input_int > 0:
        # print(input_int % 10)
        output_str.append(chr(ord("0") + input_int % 10))
        input_int //= 10

    output_str = "".join(output_str[::-1])
    if is_negative:
        return "-" + output_str
    else:
        return output_str


print(int_to_str(123))
print(int_to_str(-123))
>>> "123"
>>> "-123"

rel

7265 silver badges18 bronze badges

answered Aug 13, 2021 at 14:56

Python takes input as a string So you do not need to write a function for it When you take input num It is already a string

answered Jun 11, 2017 at 21:44

2

Your function doesn't work because you're doing a couple of things wrong. First, it will crash on the tagged python-3.x as it doesn't evaluate the input so you're essentially passing a string to your function as input() returns a string. You should use:

num = int(input("Enter any numeric value: "))  # and you should use raw_input() on Python 2.x

instead. You may want to validate the input first, too, to make sure that the integer was selected. Second, it always declares your num as negative as if num: will always return True for every value other than 0, so use if num < 0: instead.

Also, you're adding more conditions to it than needed, you can use your value in the while statement directly, i.e.:

s = [0 if not num else '']
while num:
    s.append(chr(48 + num % 10))
    num //= 10

answered Jun 11, 2017 at 22:05

How do you convert int to string without using library functions in python?

zwerzwer

24k3 gold badges42 silver badges59 bronze badges

def int_to_str(num):
    s = []
    while True:
            digit = num % 10
            num = num // 10
            if digit == num: # or we could also use digit == 0 condition 
                break
            s.append(chr(ord('0') + digit))
    return ''.join(s[::-1])

answered Oct 3, 2021 at 6:16

1

Solution using recursion.

def integer_to_string(i):

    # Edge case
    if i == 0:
        return chr(i+48)

    # Handling negative number
    negative_integer = False
    if i < 0:
        negative_integer = True
        i = - i

    def convert_to_string(i):
        if i < 10:
            return chr(i+48)
        else:
            return (convert_to_string(i // 10)) + chr((i%10)+48)

    result = convert_to_string(i)

    if negative_integer:
        return '-' + result
    else:
        return result

answered Nov 17, 2021 at 7:17

How do you convert int to string without using library functions in python?

This will work for you

def int_to_str(num):
is_negative = False
if num:
    num, is_negative = num, True
s = []
while True:
    s.append(chr(ord('0') + num % 10))
    num //= 10
    if num == 0:
        break
return ''.join(reversed(s))

answered Aug 28, 2018 at 15:36

1

How do you convert int to string in Python?

In Python an integer can be converted into a string using the built-in str() function. The str() function takes in any python data type and converts it into a string. But use of the str() is not the only way to do so. This type of conversion can also be done using the “%s” keyword, the .

How do you convert an integer to a string without using any built

Using __str__() to convert an integer to string in python So, we can directly call the __str__() on the object.

How do you convert int to string manually?

Conversion of an integer into a string by using to_string() method. The to_string() method accepts a single integer and converts the integer value or other data type value into a string.

How do you turn an int into a string?

Java int to String Example using String..
int i=10;.
String s=String.valueOf(i);//Now it will return "10".