3

I wanna ask how to convert an integer into string without using in-built function.

This is the original question:

Write a function string(ls) that returns a string representation of the list ls.

Note: do not use the built-in str() method for this task. We are attempting to emulate its behavior.

s = string(['a','b','c'])   # '['a','b','c']'

s = string([1,2,3])         # '[1, 2, 3]'

s = string([True])          # '[True]'

s = string([])              # '[]'

Restrictions: Don't just return str(ls)! Don't use the str.join method, don't use slicing.

Here is my code:

def string(ls):
    if len(ls)==0:
        mess="'[]'"
        return mess
    elif isinstance(ls[0],str):
        i=0
        mess="'["
        while True:
            if i==len(ls)-1:
                elem="'"+ls[i]+"'"
                mess=mess+elem
                break
            else:
                elem="'"+ls[i]+"', "
                mess=mess+elem
            i=i+1
        mess=mess+"]'"
        return mess
    else:
        i=0
        mess="'["
        while True:
            if i==len(ls)-1:
                elem=str(ls[i])+"]'"
                mess=mess+elem
                break
            else:
                elem=str(ls[i])+', '
                mess=mess+elem
            i=i+1
        return mess
11
  • 3
    And what is your question? Commented Sep 24, 2018 at 10:58
  • @ThierryLathuille Sorry. I wanna ask how to convert an integer into string without using in-built function? Commented Sep 24, 2018 at 11:01
  • 3
    Is there something wrong with the code you posted? Commented Sep 24, 2018 at 11:02
  • You may do: def String(List): return List.__str__(). This is a bit of a hack, but it doesn’t use the str function, does it? ;) Commented Sep 24, 2018 at 11:03
  • @Aran-Fey I write my code using in-built function str(). But the question request me not use these kind of functions. Are there some ways to solve this problem? Commented Sep 24, 2018 at 11:04

2 Answers 2

4

You can keep dividing a given integer by 10 and prepending the remainder to the output string. Use the ordinal number of '0' plus the remainder to obtain the ordinal number of the remainder, and then convert it to string using the chr function:

def int_to_string(i):
    string = ''
    while True:
        i, remainder = divmod(i, 10)
        string = chr(ord('0') + remainder) + string
        if i == 0:
            break
    return string

so that:

print(int_to_string(0))
print(int_to_string(5))
print(int_to_string(65))
print(int_to_string(923))

would output:

0
5
65
923
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent! Thank u!
0

This should work? I am fairly new so i do not know why your codes are so complicated.This should work too.

def int_to_string(i):
string = chr(ord("0") + i)
return string

1 Comment

The code which you provided is only work for single-digit integers, it is not working for numbers like 10 or 44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.