472,787 Members | 1,552 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,787 software developers and data experts.

Inconsistent behavior of split on empty string


I think the following behavior of the build in function 'split' is inconsistent.
What do you think?
"".split() [] "".split(";")


I'm using python 2.3.3 on Windows 2000.
(Perl's split only returns all items up to the last non-empty item,
e.g. <perl>split /;/, "; ;;;"; gets you ['', ' ']. I find this confusing, too).
Jul 18 '05 #1
1 2034
On 13 Feb 2004 00:46:00 -0800, bk***@gmx.de (bk***@gmx.de) wrote:

I think the following behavior of the build in function 'split' is inconsistent.
What do you think?
"".split()[] "".split(";")['']

I'm using python 2.3.3 on Windows 2000.
(Perl's split only returns all items up to the last non-empty item,
e.g. <perl>split /;/, "; ;;;"; gets you ['', ' ']. I find this confusing, too).

..split() is not an information-preserving split. It is a convenient special case
whose function should not be confused with that of .split(something).

The latter would be buggy if you could not successfully do this:
anystring = ''
somestring = ';'
assert anystring == somestring.join(anystring.split(somestring))
anystring == somestring.join(anystring.split(somestring)) True

Unfortunately (other than making a likely bug announce itself),
somestring = ''
anystring == somestring.join(anystring.split(somestring))

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: empty separator

but otherwise I think it works.

Bengt Richter
Jul 18 '05 #2

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

Similar topics

by: Robin Munn | last post by:
How is re.split supposed to work? This wasn't at all what I expected: $ python Python 2.2.2 (#1, Jan 12 2003, 12:07:20) on linux2 Type "help", "copyright", "credits" or "license" for more...
by: Will McGugan | last post by:
Hi, I'm curious about the behaviour of the str.split() when applied to empty strings. "".split() returns an empty list, however.. "".split("*") returns a list containing one empty string. ...
by: jhcorey | last post by:
I don't know where the actual issue is, but hopefully someone can explain. The following displays "5" in FireFox, but "3" in IE: <script type="text/javascript" language="javascript"> var...
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
by: Elhanan | last post by:
hi all.. i have the following string: 200850625~01~464~^^200850625~01~464~^^200850625~01~908~^^ which i will need to turn to a mutli-dimentional string array i used result.Split(new...
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf) Dim csvColumns2 As String() =...
by: Michele Petrazzo | last post by:
Hello ng, I don't understand why split (string split) doesn't work with the same method if I can't pass values or if I pass a whitespace value: >>> "".split() >>> "".split(" ") But into...
by: mad.scientist.jr | last post by:
I am working in C# ASP.NET framework 1.1 and for some reason Regex.Split isn't working as expected. When trying to split a string, Split is returning an array with the entire string in element ...
by: Stevo | last post by:
If you split a string into an array using the split method, it's not working the way I'd expect it to. That doesn't mean it's wrong of course, but would anyone else agree it's working somewhat...
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.