Will someone explain this to me? "test".find ("")
0
Why is the empty string found at position 0?
Thanks!
jw 8 2137
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****@telkoms a.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****@telkoms a.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, i don't know but
nevermind that) but what if a searched_string == '' ? Then
string.find(substring,searched_string) == 0 ?
example:
|
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 describe if applied to a sequence of length
items. It returns a tuple of three integers; respectively
these are the /start/ and /stop/ indices and the /step/ or
stride length of the slice. Missing or out-of-bounds indices
are handled in a manner...
|
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 boxes it works
fine, but if there is a number missing I get the following error:
An unhandled exception of type 'System.InvalidCastException' occurred
in microsoft.visualbasic.dll
Additional information: Cast from string "" to type 'Double' is...
|
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
string.length property). String's contents seem be teh same, so I think
there is some encoding difference (is this possibile?). The problem is that
I sign this string, so signature validation fails :(
What can be the problem?
|
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 be dependant on which string is searched for.
On my system, using gcc 4.1.2, I get:
pos1=2
problem because pos1=0
|
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;
loc = str.find( "string" );
loc = str.find( "string", loc + 1, 6 );
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
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...
| |