I'm studying python and I'll do a lot of research, but I'll always research the same thing, so I'll put it together as a memo for myself.
This is the first post. I would be grateful if you could point out anything strange.
This section summarizes python strings and path-related functions.
os.path.join
I think join () is a common function that joins arrays etc. I understand that os.path.join () is a function that joins paths.
For example
osp.join("dir1/", "dir2")
osp.join("dir1", "dir2")
Both
dir1/dir2
Returns.
There seem to be the following two main usages.
osp.join(path1, path2)
osp.join(path1 + path2)
It seems that the behavior changes when "/" comes at the beginning of the argument.
path1 = "path1"
path2 = "/path2"
osp.join(path1, path2) # /returns path2
osp.join(path1 + path2) # path1/path2
By the way, one book used it properly, but the benefits are still unknown. .. ..
If you want to use a list, add an asterisk. Reference: https://www.sejuku.net/blog/64408
For the python asterisk, see below. https://qiita.com/LouiS0616/items/1bbe0a9bb93054f6c380
format
Replace the argument of format () with the location defined by {}.
print("{}Is{}It's a yen".format("beer", 1000))
# >Beer is 1000 yen
You can also specify keywords.
print("{item}Is{price}It's a yen".format(price=2000, item="sashimi"))
# >Sashimi is 2000 yen
You can also use the dict type.
price_list = {"item":"water", "price":500}
print("{item}Is{price}It's a yen".format(**price_list))
# >Water is 500 yen
There is a description that it is not recommended so much, but since it was a description such as a book, I will leave it.
Replaces what is defined with% s,% i, etc. with the variable after%. I understand it's like sprintf.
test_template = "%s is%i yen"
print((test_template % ("ramen",1050)))
# >Ramen is 1050 yen
I will update what I have investigated again.
Recommended Posts