John Stud
John Stud

Reputation: 1779

Python: How to include \r in print output?

How can I format my string such that \r is included in the output rather than being interpreted as a carriage return?

I am building a long string with formatting entities in it like \n and \t, hence I want to preserve the printing output that I get with the below.

s1 = ("""
        WITH (
            DATA_SOURCE = 'MyAzureStorage',
            FORMAT = 'CSV',
            FIRSTROW = 2,
            FIELDTERMINATOR=',',
            DATAFILETYPE='char',
            ROWTERMINATOR='\r',
            TABLOCK)
            """)
print(''.join(s1))

Upvotes: 0

Views: 537

Answers (2)

md2perpe
md2perpe

Reputation: 3061

Use an r-string:

s1 = r"""
        WITH (
            DATA_SOURCE = 'MyAzureStorage',
            FORMAT = 'CSV',
            FIRSTROW = 2,
            FIELDTERMINATOR=',',
            DATAFILETYPE='char',
            ROWTERMINATOR='\r',
            TABLOCK)
"""
print(''.join(s1))

Upvotes: 1

Ashish M J
Ashish M J

Reputation: 700

Use repr function it returns a string containing a printable representation of an object.

s1 = ("""
        WITH (
            DATA_SOURCE = 'MyAzureStorage',
            FORMAT = 'CSV',
            FIRSTROW = 2,
            FIELDTERMINATOR=',',
            DATAFILETYPE='char',
            ROWTERMINATOR='\r',
            TABLOCK)
            """)
print(repr(s1))

Or use additional backward slash

ROWTERMINATOR='\\r',

Upvotes: 1

Related Questions