Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strings/join.py is incorrect #12408

Open
osman-haider opened this issue Dec 3, 2024 · 2 comments · May be fixed by #12410, #12411, #12434 or #12438
Open

strings/join.py is incorrect #12408

osman-haider opened this issue Dec 3, 2024 · 2 comments · May be fixed by #12410, #12411, #12434 or #12438
Labels

Comments

@osman-haider
Copy link

Repository commit

3b1b70b

Python version (python --version)

Python 3.10

Dependencies version (pip freeze)

n/a

Expected behavior

s = ["", "", ""]
join(",", s) == ",".join(s)

',,' == ',,'

Actual behavior

s = ["", "", ""]
join(",", s) == ",".join(s)

'' != ',,'

This was referenced Dec 3, 2024
@aarthipv aarthipv linked a pull request Dec 3, 2024 that will close this issue
15 tasks
@RajdeepBakolia2004 RajdeepBakolia2004 linked a pull request Dec 13, 2024 that will close this issue
@sanks011 sanks011 linked a pull request Dec 16, 2024 that will close this issue
15 tasks
@sanks011
Copy link

@osman-haider Please take a look at the merge requests

@niralinayak
Copy link

The issue arises because you may have redefined or implemented your own version of the join function that does not behave as expected. The Python built-in str.join method works correctly, and you should ensure it is used instead of any custom implementation.

Here’s how you can fix the issue:

Check for redefinition of join: Ensure that the name join hasn’t been overridden in your code or by a module.

Solution code snippet:

s = ["", "", ""]
result = ",".join(s)
print(result) # Outputs: ',,'
If you need a custom implementation of join: Ensure it mimics the behavior of str.join. Here’s an example:

def join(separator, iterable):
return separator.join(iterable)

s = ["", "", ""]
print(join(",", s)) # Outputs: ',,'
Explanation of Built-in Behavior
The built-in str.join works by concatenating the strings in the iterable with the separator. An empty string "" is still a valid string, so ",".join(["", "", ""]) produces ",,".

If join is not behaving as expected:

Check if join has been redefined by adding a print statement: print(join).
Avoid using the name join for any variable or function that isn’t related to its intended use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment