-2

I want to a store a long string (specifically a SQL query) into a variable, which I'd like to have written on more row for a better readability.

I'm using Jupyter notebook on Python 3.5 (Anaconda) if that's important.

I've tried:

# SQL query

query = "
SELECT
 Sued 
,ApplicationNumber_Primary
--,ApprovedLoanAmount, ApprovedLoanDuration, ApprovedMonthlyPayment, 
,RequiredLoanDuration, RequiredMonthlyPaymentAmount, RequiredPaidAmount, RequiredCoefficientK1
,ClientFreeSources, ClientTotalIncome, ClientNetIncome, ClientTotalExpenditures 
,ClientAgeToApplicationDate, ClientFamilyStatusID, ClientEmploymentDuration, CreditExposure
,CalendarQuarter, MonthOfYear, WeekOfYear, DayOfMonth, DayOfWeek, RegionID, DistrictID, ZIPcodeID 

FROM 
dbo.vRisk
GO
"

...which does not store the string into the variable, as I'd like.

Help would be appreciated.

Stanislav Jirak
  • 725
  • 1
  • 7
  • 22

2 Answers2

2

Try using triple quotes:

query = """
SELECT
 Sued 
,ApplicationNumber_Primary
--,ApprovedLoanAmount, ApprovedLoanDuration, ApprovedMonthlyPayment, 
,RequiredLoanDuration, RequiredMonthlyPaymentAmount, RequiredPaidAmount, RequiredCoefficientK1
,ClientFreeSources, ClientTotalIncome, ClientNetIncome, ClientTotalExpenditures 
,ClientAgeToApplicationDate, ClientFamilyStatusID, ClientEmploymentDuration, CreditExposure
,CalendarQuarter, MonthOfYear, WeekOfYear, DayOfMonth, DayOfWeek, RegionID, DistrictID, ZIPcodeID 

FROM 
dbo.vRisk
GO
"""
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Raphael
  • 1,731
  • 2
  • 7
  • 23
0

Use multi-line strings or intersperse '\n' in it:

this_is_a_multiline_string = """

tata

"""

this_as_well = '''

tata

'''

and_this = "\ntata\n"

Python.org string documentation


and_like_so = ("Some string"      # no space after
               "that spans lots"  # no space after
               "of lines" )       # results in 'Some stringthat spans lotsof lines'

(Source for the last one: https://stackoverflow.com/a/10660443/7505395 )

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69