473,664 Members | 2,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can i compare a string which is non null and empty


Hi,

how can i compare a string which is non null and empty?
i look thru the string methods here, but cant find one which does it?

http://docs.python.org/lib/string-me...string-methods

In java,I do this:
if (str != null) && (!str.equals("" )) ....

how can i do that in python?

Apr 1 '07 #1
9 10060
Pl********@gmai l.com wrote:
>
Hi,

how can i compare a string which is non null and empty?
i look thru the string methods here, but cant find one which does it?

http://docs.python.org/lib/string-me...string-methods

In java,I do this:
if (str != null) && (!str.equals("" )) ....

how can i do that in python?
What about

if str != "":
pass

?
Apr 1 '07 #2
Pl********@gmai l.com schrieb:
Hi,

how can i compare a string which is non null and empty?
i look thru the string methods here, but cant find one which does it?

http://docs.python.org/lib/string-me...string-methods

In java,I do this:
if (str != null) && (!str.equals("" )) ....

how can i do that in python?
Strings cannot be "null" in Python.

If you want to check if a string is not empty, use "if str".

This also includes the case that "str" may not only be an empty
string, but also None.

Georg

Apr 1 '07 #3
eC
On Apr 2, 12:22 am, "Plisske...@gma il.com" <Plisske...@gma il.com>
wrote:
Hi,

how can i compare a string which is non null and empty?

i look thru the string methods here, but cant find one which does it?

http://docs.python.org/lib/string-me...string-methods

In java,I do this:
if (str != null) && (!str.equals("" )) ....

how can i do that in python?
The closest to null in python is None.
Do you initialise the string to have a value of None? eg myStr = None

if so, you can test with
if myStr == None:
dosomething...

But you might find that you do not need to use None - just initialise
the string as empty eg myStr = ''
and then test for
if myStr != '':
or even simpler

if myStr:
dosomething

btw. dont use 'str' - its a built in function of python

Apr 1 '07 #4
str != ""

returns true if str is NOT the empty string.
str is not None

returns true if str is null (or None as it's called in python).

To check to make sure a string is nonnull and nonempty, do:

str is not None and str != ""

Apr 2 '07 #5
On 2007-04-01, Pl********@gmai l.com <Pl********@gma il.comwrote:
how can i compare a string which is non null and empty?
[...]
In java,I do this:
if (str != null) && (!str.equals("" )) ....

how can i do that in python?
If you want to litterally do that, it's

if (str != None) and (str != ""):
<whatever>

However, the standard idiom for doing that in Python is

if str:
<whatever>
--
Grant Edwards grante Yow! I'm RELIGIOUS!! I
at love a man with a
visi.com HAIRPIECE!! Equip me with
MISSILES!!
Apr 2 '07 #6
It is probably worth noting that the example is not executable code:
str() is a function.
sgeiger3@nycsr0 002:~$ python
Python 2.4.4c0 (#2, Jul 30 2006, 15:43:58)
[GCC 4.1.2 20060715 (prerelease) (Debian 4.1.1-9)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
>>if str: print "whoah, str is a function!"
....
whoah, str is a function!
>>>

/me pines for the day when all examples are executable code


Grant Edwards wrote:
On 2007-04-01, Pl********@gmai l.com <Pl********@gma il.comwrote:

>how can i compare a string which is non null and empty?
[...]
>In java,I do this:
if (str != null) && (!str.equals("" )) ....

how can i do that in python?

If you want to litterally do that, it's

if (str != None) and (str != ""):
<whatever>

However, the standard idiom for doing that in Python is

if str:
<whatever>
--
Shane Geiger
IT Director
National Council on Economic Education
sg*****@ncee.ne t | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
Apr 2 '07 #7
On 2007-04-02, Shane Geiger <sg*****@ncee.n etwrote:
>>how can i compare a string which is non null and empty?
[...]
>>In java,I do this:
if (str != null) && (!str.equals("" )) ....

how can i do that in python?

If you want to litterally do that, it's

if (str != None) and (str != ""):
<whatever>

However, the standard idiom for doing that in Python is

if str:
<whatever>

It is probably worth noting that the example is not executable code:
str() is a function.
Yea, sorry about that.

--
Grant Edwards grante Yow! Now I'm concentrating
at on a specific tank battle
visi.com toward the end of World
War II!
Apr 2 '07 #8
On Mon, 02 Apr 2007 01:35:17 +0200, Georg Brandl wrote:
Pl********@gmai l.com schrieb:
>Hi,

how can i compare a string which is non null and empty?
i look thru the string methods here, but cant find one which does it?

http://docs.python.org/lib/string-me...string-methods

In java,I do this:
if (str != null) && (!str.equals("" )) ....

how can i do that in python?

Strings cannot be "null" in Python.

If you want to check if a string is not empty, use "if str".

I tried that, and I get something unexpected.
>>if str:
.... print "What's going on here?"
.... else:
.... print "An empty string."
....
What's going on here?
This also includes the case that "str" may not only be an empty
string, but also None.
What about the case where str hasn't been shadowed and is a built-in type?
>>str
<type 'str'>


--
Steven D'Aprano

Apr 2 '07 #9
Pl********@gmai l.com a écrit :
Hi,

how can i compare a string which is non null and empty?
Compare with what ?-)
>
i look thru the string methods here, but cant find one which does it?

http://docs.python.org/lib/string-me...string-methods

In java,I do this:
if (str != null) && (!str.equals("" )) ....

how can i do that in python?
1/ don't use 'str' as an identifier as it will shadow the builtin str type.

2/ just test:

if some_str:
# code here

In a boolean context, both the None object (the closer equivalent to
Java's 'null') and an empty string will eval to False (FWIW, so will do
an empty list, an empty tuple, an empty set, an empty dict, and a
numeric zero).

FWIW, Python allows (and encourage where it makes sens) operator
overloading. cf:
http://docs.python.org/ref/specialnames.html

HTH
Apr 2 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
9506
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I have tried to print out by debug.writeline(). But the "==" operator results false, and string.Compare() results true. Somebody helps me!
17
8224
by: bengamin | last post by:
Hi, I have a C# class and two instance of the class; the class have some property. I want to compare the property value of the two instance How should i do? override == ? use delegate ?
3
5881
by: Asha | last post by:
greetings, i have some questions below, what are the differences between private string _strVal = string.Empty; and _strVal = null; does the string.Empty; allocate memory for it? how about _strVal = "" thanks for enlightening me on this.
35
5817
by: jacob navia | last post by:
Hi guys! I like C because is fun. So, I wrote this function for the lcc-win32 standard library: strrepl. I thought that with so many "C heads" around, maybe we could improve it in a collective brainstorming session. Let's discuss some C here, for a change :-)
10
14477
by: lchian | last post by:
Hi, For two stl strings s1 and s2, I got different results from strcmp(s1.c_str(), s2.c_str()) and s1.compare(s2) can someone explain what these functions do? It seems that strcmp gives the "right" (i.e.wysiwyg) answer while compare() does not.
12
29445
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as follows: function doDateCheckNow(source, args) { var oDate = document.getElementById(source.controltovalidate); // dd/mm/yyyy
4
6658
by: Lamis | last post by:
Hi, what is the best way to compare 2 haschtables contatining objects. the objects has 2 property, name & value. I need to print out the differences -- LZ
26
3796
by: Neville Lang | last post by:
Hi all, I am having a memory blank at the moment. I have been writing in C# for a number of years and now need to do something in VB.NET, so forgive me such a primitive question. In C#, I test whether a string has a value or not by the following syntax: if (thisString.Trim() == "") {
10
9054
by: lovecreatesbea... | last post by:
Is it correct and safe to compare a string object with "", a pair of quotation marks quoted empty string?If the string object: s = ""; does s contain a single '\'? Is it better to use std::string::size or std::string::empty to deal with this condition? Thank you. string s = ""; if (s == ""); if (s.size == 0); if (s.empty());
0
8348
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8861
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7375
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4185
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2764
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1759
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.