-1

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.

2
  • Use ''' (triple-quotes) instead of double-quotes Commented Apr 20, 2019 at 9:41
  • Of course! I thought I would just comment the statement out, but that obviously does not apply when putting it within a variable. Commented Apr 20, 2019 at 10:02

2 Answers 2

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
"""
Sign up to request clarification or add additional context in comments.

Comments

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 )

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.