0

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?

3
  • 3
    "What have I been doing wrong?" Nothing. That's how strings are printed when using print. What significance do the ' have here? If you want the ' printed use print repr(x). Commented Jul 18, 2017 at 12:20
  • 1
    '' defines a string literal but is not part of the string's characters. By default, print only prints the contained characters. Commented Jul 18, 2017 at 12:20
  • when you print your string x using print function it will not include quotes. it just prints string's value only. But you can see whether value is string or int by checking print type(x). Commented Jul 18, 2017 at 12:21

1 Answer 1

5

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).

Sign up to request clarification or add additional context in comments.

Comments

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.