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

  • Home
  • SEARCH
  • 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 9156845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:52:06+00:00 2026-06-17T12:52:06+00:00

I accidentally found that in python, an operation of the form string1.join(string2) Can be

  • 0

I accidentally found that in python, an operation of the form

string1.join(string2)

Can be equivalently expressed as

string2.replace('', string1)[len(string1):-len(string1)]

Furthermore, after trying timeit with a few different sized inputs, this weird way to join seems to be more than twice as fast.

  1. Why should the join method be slower?
  2. Is replacing the empty string like this a safe/well-defined thing to do?
  • 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-06-17T12:52:07+00:00Added an answer on June 17, 2026 at 12:52 pm

    So first of all, let’s break down why this works.

    >>> string1 = "foo"
    >>> string2 = "bar"
    >>> string1.join(string2)
    'bfooafoor'
    

    This is the operation of putting string1 between every item (character) of string2.

    So replacing the empty string does something kind of interesting, it counts the gap between empty characters as the empty string and therefore does essentially the same task, except with an extra separator at the start and end:

    >>> string2.replace('', string1)
    'foobfooafoorfoo'
    

    So slicing out these produces the same result as str.join():

    >>> string2.replace('', string1)[len(string1):-len(string1)]
    'bfooafoor'
    

    Obviously, this solution is much, much less readable than str.join(), and so I’d always recommend against it. str.join() has also been developed to be efficient on all platforms. Replacing the empty string might be far less efficient on some versions of Python (I don’t know if that’s the case, but it’s a possibility – just as repeated concatenation is reasonably fast in CPython, but that’s not necessarily the case elsewhere.)

    I can’t even find anything in the documentation that suggests that this behaviour of replacing the empty string should function this way, the docs for str.replace() simply say:

    Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

    I see no reason why we should presume that the gaps in between letters should count as an occurrence of the empty string (arguably, you could fit infinite empty strings anywhere in the string), and as such, relying on this behaviour might be a bad idea.

    This operation is also pretty rare – it’s more common to have a sequence of strings to join together – joining individual characters of a string isn’t something I have personally had to do often.

    Interestingly, this x.replace("", y) appears to be special cased in the Python source:

    2347 /* Algorithms for different cases of string replacement */
    2348
    2349 /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */
    2350 Py_LOCAL(PyStringObject *)
    2351 replace_interleave(PyStringObject *self,
    2352 const char *to_s, Py_ssize_t to_len,
    2353 Py_ssize_t maxcount)
    2354 {
    ...
    

    It may well be this special casing causes it to perform well. Again, as it’s not mentioned in the documentation, this is an implementation detail, and assuming it will work as quickly (or at all) in other Python versions would be a mistake.

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

Sidebar

Related Questions

I recently discovered that structs in C# can have methods. Quite accidentally, I found
I accidentally closed the Console window in Eclipse, and now I can't see the
I accidentally deleted my bookmarked query in phpMyAdmin. Is there any way it can
Accidentally I found this post about a new feature in ASP.NET 4.0: Expressions enclosed
I found something today in one of my databases that I cannot explain. I
I accidentally created a branch called remotes/origin/remotes/origin/aclark and can't delete it with: git branch
I found this unexpected behavior with SQLite. It appears that SQLite accepts arbitrary keywords
I accidentally deleted my firstviewcontroller.m file which is for a view that has a
I accidentally come across this page new keyword in method signature and I found
I have an application that performs various tasks that can take up to a

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.