-2

What is the best way to concatenate two triple quoted strings in python?

Example:

varA= """INSERT ....... """
VarB= """SELECT.......""""

Desired Final Query =

  """INSERT........
     SELECT........"""

When I do :

varC=varA+varB 

I get something like :

      """INSERT........

SELECT........"""

That is, I don't get the style I desire. How can I achieve this? Thanks in advance :)

3
  • 1
    Why does the indentation matter in the least? It's entirely irrelevant. Are you printing the SQL inline with Python code? How / why? If you're just sending it to your database server the indentation doesn't matter. (Note that the indentation in the first line isn't in the SQL query; it's in the surrounding Python code.) Commented Jun 4, 2017 at 23:02
  • 1
    print '{}\n{}'.format(varA, VarB) Commented Jun 4, 2017 at 23:04
  • The described problem is not reproducible. Commented Apr 22, 2024 at 21:21

2 Answers 2

-1

In python the main use of triple quotes strings are for multiline strings. Thus str1="bees" is equivalent to str1="""bees""", but

str1="""
   bees"""

is not equivalent to

str1="
   bees"

Anyway, using your two strings, if you just want to have str2 under str1, you can:

str1 = """INSERT......."""
str2 = """SELECT......."""

str3 = str1 + "\n" + str2

# >>> str1 = """INSERT......."""
# >>> str2 = """SELECT......."""
# >>> print str1 + "\n" + str2
# INSERT.......
# SELECT.......

or

print "{0}\n{1}".format(str1, str2)
Sign up to request clarification or add additional context in comments.

Comments

-1

Are you using print() to show varC? Try this,

varA = """INSERT ....... """

varB = """SELECT......."""

varC = varA + '\n' + varB

print(varC)

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.