Shyama Sonti
Shyama Sonti

Reputation: 331

Putting double quotes for an output string

I have these two lines of code:

outfile="C:\Temp\gens.csv"
print("SaveData("  + outfile + ",CSV,GEN,[BusNum, ID, MW, MVAR, VoltSet], [])")

The output is

SaveData(C:\Temp\gens.csv,CSV,GEN,[BusNum, ID, MW, MVAR, VoltSet], [])

But would like to see output like this...double quotes around the path.

SaveData("C:\Temp\gens.csv",CSV,GEN,[BusNum, ID, MW, MVAR, VoltSet], [])

Upvotes: 1

Views: 930

Answers (2)

Right leg
Right leg

Reputation: 16740

You must format your string so the double quotes are actually printed. Here, you have several solutions.

  • Escape the double quotes with a backslash: "these too \" will be printed"
  • Use single quotes to mark your string: 'these double quotes " will be printed'
  • Use triple quotes to mark your string:
    • """You can print "s or 's here without having to escape either"""
    • '''And this "works" 'as' well'''

The main advantage of triple quotes is that you rarely want to display triple quotes, so you won't ever have to bother with escaping.

Using simple quotes for your string can be bothersome, because ' happens to be an apostrophe too, which is pretty common in English.

In most situation, double quotes " are the best choice, since it's simple, doesn't require escaping simple quotes, and escaping a double quote is really easy ("\"").

If you happen to have a lot of textual data, I would recommend the use of triple quotes.

But anyway, in the end, all the ways to declare strings are equivalent.

Upvotes: 0

gipsy
gipsy

Reputation: 3859

print('SaveData("'  + outfile + '",CSV,GEN,[BusNum, ID, MW, MVAR, VoltSet], [])'

Upvotes: 2

Related Questions