faheem19877676
faheem19877676

Reputation: 31

python: integer to string conversion

I have been trying to convert an integer into a string. My codes are as follows:

n = 357
x = str(n)
print x

The problem is that online editors that I have tried are not printing the string as '357'. Instead the string is printed as 357. What have I been doing wrong?

Upvotes: 0

Views: 698

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476493

You apparently want to print the representation of the string. Python has a builtin function repr(..) for this:

n = 357
x = str(n)
print(repr(x))

The representation of a string is a list of characters between single or double quotes (double quotes if the string contains at least one single quote and no double quotes). Furthermore if there are escape sequences (like a new line), these are printed with a backslash (like \n).

Upvotes: 5

Related Questions