👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
For example, I want to join a prefix path to resource paths like /js/foo.js.
I want the resulting path to be relative to the root of the server. In the above example if the prefix was "media" I would want the result to be /media/js/foo.js.
os.path.join does this really well, but how it joins paths is OS dependent. In this case I know I am targeting the web, not the local file system.
Is there a best alternative when you are working with paths you know will be used in URLs? Will os.path.join work well enough? Should I just roll my own?
👻 Read also: what is the best laptop for engineering students?
Why is it string.join(list) instead of list.join(string)?
5 answers
This has always confused me. It seems like this would be nicer:
my_list = ["Hello", "world"]
print(my_list.join("-"))
# Produce: "Hello-world"
Than this:
my_list = ["Hello", "world"]
print("-".join(my_list))
# Produce: "Hello-world"
Is there a specific reason it is like this?
Answer #1
It"s because any iterable can be joined (e.g, list, tuple, dict, set), but its contents and the "joiner" must be strings.
For example:
"_".join(["welcome", "to", "stack", "overflow"])
"_".join(("welcome", "to", "stack", "overflow"))
"welcome_to_stack_overflow"
Using something other than strings will raise the following error:
TypeError: sequence item 0: expected str instance, int found
Answer #2
This was discussed in the String methods... finally thread in the Python-Dev achive, and was accepted by Guido. This thread began in Jun 1999, and str.join
was included in Python 1.6 which was released in Sep 2000 (and supported Unicode). Python 2.0 (supported str
methods including join
) was released in Oct 2000.
- There were four options proposed in this thread:
str.join(seq)
seq.join(str)
seq.reduce(str)
join
as a built-in function
- Guido wanted to support not only
list
s andtuple
s, but all sequences/iterables. seq.reduce(str)
is difficult for newcomers.seq.join(str)
introduces unexpected dependency from sequences to str/unicode.join()
as a built-in function would support only specific data types. So using a built-in namespace is not good. Ifjoin()
supports many datatypes, creating an optimized implementation would be difficult, if implemented using the__add__
method then it would veO(n²)
.- The separator string (
sep
) should not be omitted. Explicit is better than implicit.
Here are some additional thoughts (my own, and my friend"s):
- Unicode support was coming, but it was not final. At that time UTF-8 was the most likely about to replace UCS2/4. To calculate total buffer length of UTF-8 strings it needs to know character coding rule.
- At that time, Python had already decided on a common sequence interface rule where a user could create a sequence-like (iterable) class. But Python didn"t support extending built-in types until 2.2. At that time it was difficult to provide basic
iterable
class (which is mentioned in another comment).
Guido"s decision is recorded in a historical mail, deciding on str.join(seq)
:
Funny, but it does seem right! Barry, go for it...
Guido van Rossum
Answer #3
Because the join()
method is in the string class, instead of the list class?
I agree it looks funny.
See http://www.faqs.org/docs/diveintopython/odbchelper_join.html:
Historical note. When I first learned Python, I expected join to be a method of a list, which would take the delimiter as an argument. Lots of people feel the same way, and there’s a story behind the join method. Prior to Python 1.6, strings didn’t have all these useful methods. There was a separate string module which contained all the string functions; each function took a string as its first argument. The functions were deemed important enough to put onto the strings themselves, which made sense for functions like lower, upper, and split. But many hard-core Python programmers objected to the new join method, arguing that it should be a method of the list instead, or that it shouldn’t move at all but simply stay a part of the old string module (which still has lots of useful stuff in it). I use the new join method exclusively, but you will see code written either way, and if it really bothers you, you can use the old string.join function instead.
--- Mark Pilgrim, Dive into Python
We hope this article has helped you to resolve the problem. Apart from How to join components of a path when you are constructing a URL in Python, check other COM PHP module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano How to join components of a path when you are constructing a URL in Python
- Deutsch How to join components of a path when you are constructing a URL in Python
- Français How to join components of a path when you are constructing a URL in Python
- Español How to join components of a path when you are constructing a URL in Python
- Türk How to join components of a path when you are constructing a URL in Python
- Русский How to join components of a path when you are constructing a URL in Python
- Português How to join components of a path when you are constructing a URL in Python
- Polski How to join components of a path when you are constructing a URL in Python
- Nederlandse How to join components of a path when you are constructing a URL in Python
- 中文 How to join components of a path when you are constructing a URL in Python
- 한국어 How to join components of a path when you are constructing a URL in Python
- 日本語 How to join components of a path when you are constructing a URL in Python
- हिन्दी How to join components of a path when you are constructing a URL in Python
Texas | 2023-04-01
Thanks for explaining! I was stuck with How to join components of a path when you are constructing a URL in Python for some hours, finally got it done 🤗. I am just not quite sure it is the best method
Prague | 2023-04-01
Simply put and clear. Thank you for sharing. How to join components of a path when you are constructing a URL in Python and other issues with resource Python module was always my weak point 😁. Checked yesterday, it works!
California | 2023-04-01
Simply put and clear. Thank you for sharing. How to join components of a path when you are constructing a URL in Python and other issues with Python functions was always my weak point 😁. Will get back tomorrow with feedback