Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #65678 > unrolled thread
| Started by | Sam <lightaiyee@gmail.com> |
|---|---|
| First post | 2014-02-08 06:02 -0800 |
| Last post | 2014-02-10 10:50 +0000 |
| Articles | 6 — 6 participants |
Back to article view | Back to comp.lang.python
What does """ means in python? Sam <lightaiyee@gmail.com> - 2014-02-08 06:02 -0800
Re: What does """ means in python? Roy Smith <roy@panix.com> - 2014-02-08 09:08 -0500
Re: What does """ means in python? Walter Hurry <walterhurry@gmail.com> - 2014-02-08 19:55 +0000
Re: What does """ means in python? Chris Angelico <rosuav@gmail.com> - 2014-02-09 08:40 +1100
Vedr: What does """ means in python? Gisle Vanem <gvanem@yahoo.no> - 2014-02-09 05:49 +0000
Re: Vedr: What does """ means in python? Duncan Booth <duncan.booth@invalid.invalid> - 2014-02-10 10:50 +0000
| From | Sam <lightaiyee@gmail.com> |
|---|---|
| Date | 2014-02-08 06:02 -0800 |
| Subject | What does """ means in python? |
| Message-ID | <72a7dd52-7619-4520-991e-20db7ce55ba3@googlegroups.com> |
For string, one uses "" to represent string. Below is a code fragment that uses """ instead.
cursor.execute("""SELECT name, phone_number
FROM coworkers
WHERE name=%s
AND clue > %s
LIMIT 5""",
(name, clue_threshold))
What does """ means in python?
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2014-02-08 09:08 -0500 |
| Message-ID | <roy-C8B893.09080908022014@news.panix.com> |
| In reply to | #65678 |
In article <72a7dd52-7619-4520-991e-20db7ce55ba3@googlegroups.com>,
Sam <lightaiyee@gmail.com> wrote:
> For string, one uses "" to represent string. Below is a code fragment that
> uses """ instead.
>
> cursor.execute("""SELECT name, phone_number
> FROM coworkers
> WHERE name=%s
> AND clue > %s
> LIMIT 5""",
> (name, clue_threshold))
>
> What does """ means in python?
This is what's known as a "triple quoted string" It's just like a
regular string, except that it run across newlines. Very handy for
things like embedding SQL code in a Python program!
It works with single quotes too (i.e. '''this is
a very long string
spread out over several lines'''
[toc] | [prev] | [next] | [standalone]
| From | Walter Hurry <walterhurry@gmail.com> |
|---|---|
| Date | 2014-02-08 19:55 +0000 |
| Message-ID | <ld6237$ju9$1@news.albasani.net> |
| In reply to | #65679 |
Roy Smith wrote:
> In article <72a7dd52-7619-4520-991e-20db7ce55ba3@googlegroups.com>,
> Sam <lightaiyee@gmail.com> wrote:
>
>> For string, one uses "" to represent string. Below is a code fragment that
>> uses """ instead.
>>
>> cursor.execute("""SELECT name, phone_number
>> FROM coworkers
>> WHERE name=%s
>> AND clue > %s
>> LIMIT 5""",
>> (name, clue_threshold))
>>
>> What does """ means in python?
>
> This is what's known as a "triple quoted string" It's just like a
> regular string, except that it run across newlines. Very handy for
> things like embedding SQL code in a Python program!
>
> It works with single quotes too (i.e. '''this is
> a very long string
> spread out over several lines'''
PMFJI.
When I asked (here) about this a while ago, some kind soul suggested textwrap.dedent.
Any advice as to the pros and cons of the respective approaches (esp. for SQL)?
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-02-09 08:40 +1100 |
| Message-ID | <mailman.6549.1391895636.18130.python-list@python.org> |
| In reply to | #65693 |
On Sun, Feb 9, 2014 at 6:55 AM, Walter Hurry <walterhurry@gmail.com> wrote: > When I asked (here) about this a while ago, some kind soul suggested textwrap.dedent. > > Any advice as to the pros and cons of the respective approaches (esp. for SQL)? For SQL? Ignore the extra spaces, it's a free-form language. The only reason to consider dedent() would be if you're worried about how your log files will look. The actual execution of SQL won't be bothered by newlines and spaces/tabs. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Gisle Vanem <gvanem@yahoo.no> |
|---|---|
| Date | 2014-02-09 05:49 +0000 |
| Subject | Vedr: What does """ means in python? |
| Message-ID | <mailman.6570.1391925165.18130.python-list@python.org> |
| In reply to | #65693 |
Gisle V.
"Computers are useless. They can only give answers" --Pablo Picasso
Chris Angelico <rosuav@gmail.com> wrote:
> For SQL? Ignore the extra spaces, it's a free-form language. The only
> reason to consider dedent() would be if you're worried about how your
> log files will look. The actual execution of SQL won't be bothered by
> newlines and spaces/tabs.
Regrading handy uses of ''', you learned me one trick when using Python
code in a Windows .bat file:
rem = '''
@echo off
echo This is
batch
\python32\python %0
echo All done
exit /b
rem '''
import
sys
print("This is Python")
for i,p in
enumerate(sys.path):
print('sys.path[%2d]: %s' % (i, p))
print("Python
done")
You'll have a variable in Python called 'rem' which contains all
your
batch code :) It exploits the fact that 'rem' makes a
one-line
comment, but the triple quotes go across multiple lines.
[toc] | [prev] | [next] | [standalone]
| From | Duncan Booth <duncan.booth@invalid.invalid> |
|---|---|
| Date | 2014-02-10 10:50 +0000 |
| Subject | Re: Vedr: What does """ means in python? |
| Message-ID | <XnsA2D06DC06AA1Fduncanbooth@127.0.0.1> |
| In reply to | #65725 |
Gisle Vanem <gvanem@yahoo.no> wrote:
> Regrading handy uses of ''', you learned me one trick when using Pythonÿ
> code in a Windows .bat file:
>
> rem = '''
> @echo off
> echo This is batch
> \python32\python %0
> echo All done
> exit /b
> rem '''
> import sys
> print("This is Python")
> for i,p in enumerate(sys.path):
> print('sys.path[%2d]: %s' % (i, p))
> print("Python done")
> You'll have a variable in Python called 'rem' which contains all
> your
> batch code :) It exploits the fact that 'rem' makes a
> one-line
> comment, but the triple quotes go across multiple lines.
>
A better trick would be to use a Powershell script instead of a batch
file:
-------------------------------------------
filter python() { $_ | c:\Python33\python.exe ($args -replace'(\\*)"','$1$1\"') }
Write-Host "This is the powershell script"
dir cert: | convertto-json | python -c @"
import json, sys
stores = json.loads(sys.stdin.read())
print("This is Python")
for store in stores:
print("{}: {}".format(store['PSChildName'], ', '.join(store['StoreNames'])))
print("Python done")
"@
Write-Host "All done"
-------------------------------------------
C:\scripts> . .\Pythoncerts.ps1
This is the powershell script
This is Python
CurrentUser: Root, UserDS, Disallowed, Trust, My, TrustedPublisher, SmartCardRoot, TrustedPeople, ADDRESSBOOK, AuthRoot,
McAfee Trust, CA, REQUEST, ACRS
LocalMachine: Disallowed, Trust, CA, TrustedPublisher, SmartCardRoot, My, TrustedPeople, AuthRoot, TrustedDevices, Root
Python done
All done
C:\scripts>
Notes on the above:
Powershell messes up arguments when running legacy programs. The filter
ensures that all arguments pass through Windows command line processing
unscathed (except they can't contain null characters). You don't actually
have to use the filter if you are careful about how you write quotes in the
code, but it makes life simpler.
Python scripts up to just over 32,000 characters can be written on the
command line this way. You can also assign the script to a variable and
keep the Python command a bit cleaner:
$script = @"
print("Python here!")
"@
python -c $script
Or without the filter it is best to avoid the double quotes:
$script = @"
print('Python here!')
"@
c:\python33\python.exe -c $script
To run from a traditional cmd.exe prompt you have to explicitly use
Powershell. The default file associations for .ps1 files will run notepad
instead.
If your system execution policy is Restricted (the default) use:
powershell -executionpolicy RemoteSigned .\Pythoncerts.ps1
Otherwise set the execution policy to something more lenient (at a
Powershell prompt running as administrator enter "Set-ExecutionPolicy
RemoteSigned") and you can just do:
powershell .\Pythoncerts.ps1
I also use Powershell interactively so I have the filters defined in my
startup ($Home\Documents\WindowsPowerShell\profile.ps1):
filter py() { $_ | py.exe ($args -replace'(\\*)"','$1$1\"') }
filter python() { $_ | c:\Python33\python.exe ($args -replace'(\\*)"','$1$1\"') }
--
Duncan Booth
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web