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? 9 10023 Pl********@gmail.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
On Apr 2, 12:22 am, "Plisske...@gmail.com" <Plisske...@gmail.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
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 != ""
On 2007-04-01, Pl********@gmail.com <Pl********@gmail.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!!
It is probably worth noting that the example is not executable code:
str() is a function.
sgeiger3@nycsr0002:~$ 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********@gmail.com <Pl********@gmail.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.net | 402-438-8958 | http://www.ncee.net
Leading the Campaign for Economic and Financial Literacy
On 2007-04-02, Shane Geiger <sg*****@ncee.netwrote:
>>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!
On Mon, 02 Apr 2007 01:35:17 +0200, Georg Brandl wrote: Pl********@gmail.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 Pl********@gmail.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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 ?
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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,...
|
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...
| |