How to print raw string data from a variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stygian
    New Member
    • May 2007
    • 6

    #1

    How to print raw string data from a variable

    Could someone (anyone) give me a clue how to display all characters stored in a string variable.

    For example, if I have a text file containing '1\n2\n3\n'
    I read it in with A=file.read()

    Now I would like python to print A as 1\n2\n3\n and not
    1
    2
    3

    Do I have to convert A to a raw string, or does it already contain raw data that is then processed by the print statement?

    Do raw strings exist seperately from 'normal' strings, or is it just a string displayed in its entirety?

    Thanks
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by stygian
    Could someone (anyone) give me a clue how to display all characters stored in a string variable.

    For example, if I have a text file containing '1\n2\n3\n'
    I read it in with A=file.read()

    Now I would like python to print A as 1\n2\n3\n and not
    1
    2
    3

    Do I have to convert A to a raw string, or does it already contain raw data that is then processed by the print statement?

    Do raw strings exist seperately from 'normal' strings, or is it just a string displayed in its entirety?

    Thanks
    [code=Python]>>> s = '1\n2\n3\n'
    >>> print repr(s)
    '1\n2\n3\n'
    >>> [/code]

    Comment

    • stygian
      New Member
      • May 2007
      • 6

      #3
      Originally posted by bvdet
      [code=Python]>>> s = '1\n2\n3\n'
      >>> print repr(s)
      '1\n2\n3\n'
      >>> [/code]
      repr() , I've not seen that before

      Thanks that's great.

      Comment

      Working...