Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7682377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:33:17+00:00 2026-05-31T18:33:17+00:00

>>> .a string.split(‘.’) [”, ‘a string’] >>> a .string.split(‘.’) [‘a ‘, ‘string’] >>> a

  • 0
>>> ".a string".split('.')
['', 'a string']

>>> "a .string".split('.')
['a ', 'string']

>>> "a string.".split('.')
['a string', '']

>>> "a ... string".split('.')
['a ', '', '', ' string']

>>> "a ..string".split('.')
['a ', '', 'string']

>>> 'this  is a test'.split(' ')
['this', '', 'is', 'a', 'test']

>>> 'this  is a test'.split()
['this', 'is', 'a', 'test']

Why is split() different from split(' ') when the invoked string only have spaces as whitespaces?

Why split('.') splits "..." to ['','']? split() does not consider an empty word between 2 separators…

The docs are clear about this (see @agf below), but I’d like to know why is this the chosen behaviour.

I have looked in the source code (here) and thought line 136 should be just less than: …i < str_len…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:33:19+00:00Added an answer on May 31, 2026 at 6:33 pm

    See the str.split docs, this behavior is specifically mentioned:

    If sep is given, consecutive delimiters are not grouped together and
    are deemed to delimit empty strings (for example, '1,,2'.split(',')
    returns ['1', '', '2']). The sep argument may consist of multiple
    characters (for example, '1<>2<>3'.split('<>') returns ['1', '2',
    '3']
    ). Splitting an empty string with a specified separator returns
    [''].

    If sep is not specified or is None, a different splitting algorithm is
    applied: runs of consecutive whitespace are regarded as a single
    separator, and the result will contain no empty strings at the start
    or end if the string has leading or trailing whitespace
    . Consequently,
    splitting an empty string or a string consisting of just whitespace
    with a None separator returns [].

    Python tries to do what you would expect. Most people not thinking too hard would probably expect

    '1 2 3 4 '.split() 
    

    to return

    ['1', '2', '3', '4']
    

    Think about splitting data where spaces have been used instead of tabs to create fixed-width columns — if the data is different widths, there will be different number of spaces in each row.

    There is often trailing whitespace at the end of a line that you can’t see, and the default ignores it as well — it gives you the answer you’d visually expect.

    When it comes to the algorithm used when a delimiter is specified, think about a row in a CSV file:

    1,,3
    

    means there is data in the 1st and 3rd columns, and none in the second, so you would want

    '1,,3'.split(',')
    

    to return

    ['1', '', '3']
    

    otherwise you wouldn’t be able to tell what column each string came from.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this: Dim split As String() = temp_string.Split(,) ''#feed all info into global
I have something like this: string[] split = ListOfUsers.Split(new Char[] { ';', ',', '
Is C# Split Function different from Java ? Why do I get this error
I have a string like this This:string:must~:be:split:when:previous:char:is:not~:this I need to split the line with
Possible Duplicate: string split in java I have this Key - Value , and
Unfortunately, there seems to be no string.Split(string separator), only string.Split(char speparator). I want to
Possible Duplicate: Split string in SQL I have seen a couple of questions related
I need split string by commas and spaces, but ignore the inside quotes, single
Java's string split(regex) function splits at all instances of the regex. Python's partition function
I need to split this line string in each line, I need to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.