Will someone explain this to me? "test".find("")
0
Why is the empty string found at position 0?
Thanks!
jw 8 2123
Hi Jaime
I don't know why the function was set up this way. However, an empty
string can be found in an infinite number of places within any other
string. It is a difficult situation, like dividing zero into a nonzero
number. (Are you asking why it specifically returns 0 and not INF or an
exception...?)
If you want to match a *blank*, you gotta do "test".find(" ") # There is a space between the last two ""s
-1 _
but I expect you are aware of this and this was not your question. Also,
if you want to check whether a string is empty, I do
"test" == ""
False _
One of the gurus can probably answer you better than I.
thx
Caleb
On Thu, 11 Nov 2004 13:36:41 -0600, Jaime Wyant <pr***********@gmail.com>
wrote: Will someone explain this to me? "test".find("")
0
Why is the empty string found at position 0?
Thanks! jw
On 2004-11-12, Caleb Hattingh <ca****@telkomsa.net> wrote: I don't know why the function was set up this way. However, an empty string can be found in an infinite number of places within any other string.
How so? There aren't an infinite number of places _in_ a
finite-length string.
--
Grant Edwards grante Yow! Mr and Mrs PED, can I
at borrow 26.7% of the RAYON
visi.com TEXTILE production of the
INDONESIAN archipelago?
Caleb Hattingh <caleb1 <at> telkomsa.net> writes: I don't know why the function was set up this way. However, an empty string can be found in an infinite number of places within any other string.
Infinite might be an exaggeration. Since there are only a finite number of
indices into a string (len(s) + 1), there are only a finite number of places an
empty sting may be found in any given string: s = 'abc' s.find('')
0 s.find('', 1)
1 s.find('', 2)
2 s.find('', 3)
3 s.find('', 4)
-1
You can't, say, find the empty string somewhere between indices 1 and 2.
Also, if you want to check whether a string is empty, I do
"test" == ""
False
An empty string evaluates to False in a boolean context, so you probably don't
usually want to actually test like this. A couple of options:
s = '' bool(s)
False
s = '' if s:
.... print 'not empty'
.... else:
.... print 'empty'
....
empty
My suspicion is that any time you actually test against an empty string, you're
probably doing this in the context of an if statement or a while loop, so you
can simply use the string directly instead of testing anything.
Steve
Jaime Wyant wrote: Will someone explain this to me?
"test".find("")
0
Why is the empty string found at position 0?
It's found there because it is there!
At any position, you can find an empty string because it is so short...
David
Steven and Grant (Edwards)
Err...discrete math was never my strong point :) Still, matching
'emptiness' to non-emptiness is what conceptually raises the problem for
me. I am not convinced that because there are only 'so many places' to
fit, one is restricted to a finite number of possibilities - simply
because what we are trying fit is 'nothing'.
Of course, I make this claim on nothing more than a hunch and
indigestion. The field of discrete math must have resolved this kind of
thing at some point, and I'll accept that position. If that was in fact
the position you two just presented, I apologise.
thx
Caleb
On Thu, 11 Nov 2004 20:37:26 +0000 (UTC), Steven Bethard
<st************@gmail.com> wrote: Caleb Hattingh <caleb1 <at> telkomsa.net> writes: I don't know why the function was set up this way. However, an empty string can be found in an infinite number of places within any other string.
Infinite might be an exaggeration. Since there are only a finite number of indices into a string (len(s) + 1), there are only a finite number of places an empty sting may be found in any given string:
s = 'abc' s.find('') 0 s.find('', 1) 1 s.find('', 2) 2 s.find('', 3) 3 s.find('', 4) -1
You can't, say, find the empty string somewhere between indices 1 and 2.
Also, if you want to check whether a string is empty, I do
>>> "test" == "" False
An empty string evaluates to False in a boolean context, so you probably don't usually want to actually test like this. A couple of options: s = '' bool(s) False s = '' if s:
... print 'not empty' ... else: ... print 'empty' ... empty
My suspicion is that any time you actually test against an empty string, you're probably doing this in the context of an if statement or a while loop, so you can simply use the string directly instead of testing anything.
Steve
On 2004-11-12, Caleb Hattingh <ca****@telkomsa.net> wrote: Err...discrete math was never my strong point :) Still, matching 'emptiness' to non-emptiness is what conceptually raises the problem for me. I am not convinced that because there are only 'so many places' to fit, one is restricted to a finite number of possibilities - simply because what we are trying fit is 'nothing'.
Ah, but the question wasn't how many empty strings would fit,
but at how many places you could "find" one. :)
If you have three pins, then there are only three pinheads on
which you can find a dancing angel -- regardless of how many
angles can dance on the head of a pin (assuming it's not zero).
--
Grant Edwards grante Yow! I smell a RANCID
at CORN DOG!
visi.com
>>>>> "Jaime" == Jaime Wyant <pr***********@gmail.com> writes:
Jaime> Will someone explain this to me? "test".find("")
Jaime> 0
Jaime> Why is the empty string found at position 0?
It is completely natural. "test".find("es") = 1 because 1 is the
smallest x such that "test"[x:x+len("es")] == "es" (i.e., the smallest
x such that the substring of "test" from position x starts with "es").
Now what is the smallest x such that "test"[x:x+len("")] == ""?
Regards,
Isaac.
Jaime Wyant wrote: Will someone explain this to me?
"test".find("")
0
Why is the empty string found at position 0?
Think of it this way: "test".find("") returns the index of the first
position in "test" where a substring of length len("") occurs equal to "".
the-empty-set-is-a-subset-of-all-sets-ly y'rs - steve
-- http://www.holdenweb.com http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: MyHaz |
last post by:
OK i find this a quark in string.find that i think needs some
consideration.
Normally if a substring is not in the searched_string then string.find
returns -1 (why not len(searched_string) + 1,...
|
by: Bryan Olson |
last post by:
The Python slice type has one method 'indices', and reportedly:
This method takes a single integer argument /length/ and
computes information about the extended slice that the slice
object would...
|
by: ondekoza |
last post by:
Hello,
I need to convert the string "FFFFFFFF" to a long. To convert this
string I tried the following:
>>> 0xffffffff
-1
>>> 0xffffffffL
4294967295L
OK, this is what I want, so I tried
|
by: Michael |
last post by:
How can I get X (or another value) from this string
"asdfsadfX",
basically i want to get what ever is in between the tags and
place them in a variable called $www , can anyone help?
|
by: pinkfloydhomer |
last post by:
How do I check if a string contains (can be converted to) an int? I
want to do one thing if I am parsing and integer, and another if not.
/David
|
by: dc15 |
last post by:
For an intro to VB project I have to write a program which takes an
amount of Miles, Yards, and Inches.....and converts it to metric (KM,
M, and CM) when all values are entered to the input text...
|
by: Trapulo |
last post by:
I've a webservice with a string parameter.
I call this webservice passing a string that is 1129 chars length. On the
webservice, the received string is 1146 length (I tested sizes with...
|
by: Johny |
last post by:
Is there a good way how to use string.find function to find a
substring if I need to you case insensitive substring?
Thanks for reply
LL
|
by: Ko van der Sloot |
last post by:
Hello
I was wondering which behaviour might be expected (or is required) for
the following small program.
I would expect that find( "a", string::npos ) would return string::npos
but is seems to...
|
by: Soneji |
last post by:
Just a quickie today...
Is there another ( better ) way to get the second value from this string::find example:
std::string str( "More strings of the string variety" );
size_t loc;
...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |