Fedora Linux Support Community & Resources Center
  #1  
Old 5th June 2005, 12:09 AM
rantanplan Offline
Registered User
 
Join Date: Oct 2004
Location: Germany
Age: 43
Posts: 125
Thumbs up Python testing empty string

This is to aaaaaaalllllllll Fedorianer. The Universe is waiting for you. Particular Python is waiting.


Ok. The Question.
I'm new to it. I want to knwo how i can test if an argument in Python
sys.argv[1:] is empty ? Or even in general an string is empty ?
In C/C++ i use the compare against 0/NULL but this doesn't work in Python.

i wrote it this way


if sys.argv[1:] == "":
print "Please enter adress."


Btw, this is an official licenced Newbie question by ... Dumb&Nice Inc.

Thank you.
__________________
No bugs, no hugs, no virus, no spiritus. :rolleyes:
Reply With Quote
  #2  
Old 6th June 2005, 04:45 AM
Daverz Offline
Registered User
 
Join Date: May 2004
Posts: 128
Quote:
Originally Posted by rantanplan

if sys.argv[1:] == "":
print "Please enter adress."
Yes, that's how you'd test for an empty string, but this assumes that what was entered on the command line was quoted to begin with:

[dmcook@localhost ~]$ /tmp/test.py -f "" -g ""
['/tmp/test.py', '-f', '', '-g', '']

Not too likely. If they just leave off the arguments, you'll see:

['/tmp/test.py', '-f', '-g']

So checking for an empty string is not too helpful.

Instead I suggest the optparse module, which I think has been standard since 2.3. It makes adding command-line options to your programs very easy.

http://docs.python.org/lib/module-optparse.html
Reply With Quote
  #3  
Old 6th June 2005, 05:01 AM
Jman Offline
Registered User
 
Join Date: Mar 2004
Location: Minnesota, USA
Age: 28
Posts: 7,909
You can also write it
Code:
if len(string) == 0
See the sequence reference
Reply With Quote
  #4  
Old 6th June 2005, 10:38 AM
rantanplan Offline
Registered User
 
Join Date: Oct 2004
Location: Germany
Age: 43
Posts: 125
Lightbulb Very good.

This was very helpful. Thank to you all.
I have the Documentation for Python but for some kind little nifty trick it's a huge
document to look. And sometimes it's to much information for an beginner if i look
for such 'easy' things like empty strings.
Does anyone know a good Python-Forum like this awesome Fedora Board ?
(Deutsch bevorzugt.) German preferred.
__________________
No bugs, no hugs, no virus, no spiritus. :rolleyes:
Reply With Quote
  #5  
Old 11th May 2011, 07:35 PM
marko Offline
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 6,068
linuxfirefox
Re: Python testing empty string

The best practice, elegant way to test for an empty string like that would be
to replace
Quote:
if len(string) == 0:
with the shorter code:

Quote:
if not string:
REF see the Section 5.1 in the sequence reference in post 3, it shows that empty python sequences themselves resolve in the if statement in a logical way, so [], {}, or '' or None, etc --> False
Reply With Quote
  #6  
Old 8th May 2012, 07:26 PM
wattana Offline
Registered User
 
Join Date: Jan 2007
Posts: 4
linuxfirefox
Re: Python testing empty string

Thanks Marko!!

This is a great tip!!
__________________
darkness come...
________________
Digital Dreams
Reply With Quote
  #7  
Old 8th May 2012, 07:47 PM
jpollard Offline
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,870
linuxfirefox
Re: Python testing empty string

Note: there is a difference between an empty string, and no string at all.
Reply With Quote
Reply

Tags
python, string, testing

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Grace: xmgrace not working after update to KDE4.2 using updtaes-testing/testing repos cunio Using Fedora 0 9th February 2009 08:17 PM
server glx vendor string: SGI server glx version string: 1.2 akp Installation, Upgrades and Live Media 4 25th November 2007 07:04 PM
server glx vendor string: SGI, server glx version string: 1.2 + ATI X1950 akp Using Fedora 1 2nd September 2007 02:56 AM
sed regex string replacement after match string issue adosch Using Fedora 10 29th June 2007 04:07 PM
New (RH9/FC1, testing): python-adns-1.0.0-0.fdr.3 fedora-package-announce-admin@fedora.us Advisories & Updates 0 31st January 2004 09:54 AM


Current GMT-time: 01:55 (Saturday, 01-11-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
City of London Photos on Instagram - Carmen de Viboral Photos on Instagram - Chaozhou Travel Photos