[Python beginner memo] Python character string, path operation

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

% Operator

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

in conclusion

I will update what I have investigated again.

Recommended Posts

[Python beginner memo] Python character string, path operation
python beginner memo (9.2-10)
python beginner memo (9.1)
Python beginner memo (2)
Python UTC ⇔ JST, character string (UTC) ⇒ JST conversion memo
Python decorator operation memo
Python f character (formatted string)
Is it a character string operation?
[Beginner] Extract character strings with Python
Python memo
python memo
Python memo
python memo
Python memo
Python string
[Beginner memo] How to specify the library reading path in Python
Python memo
Python memo
# 5 [python3] Extract characters from a character string
Python beginner Atcoder memo @ KEYENCE 2020, ABC problem
[Python] How to invert a character string
Python data structure and operation (Python learning memo ③)
Basic grammar of Python3 system (character string)
Python basic course (4 numeric type / character string type)
Beginner ABC154 (Python)
[Python] Memo dictionary
Python: String concatenation
Beginner ABC156 (Python)
Python string format
python string slice
[python] vector operation
Python beginner notes
[Beginner] Python array
Character range / character string range
★ Memo ★ Python Iroha
Beginner ABC155 (Python)
Python OS operation
Calculation of match rate of character string breaks [python]
[Python] EDA memo
Python 3 operator memo
[Beginner] Python functions
[Python] Matrix operation
Beginner ABC157 (Python)
PyQ ~ Python Beginner ~
[My memo] python
Python2 string type
Python3 metaclass memo
Python string format
Python # string type
[Python] Basemap memo
A memo for creating a python environment by a beginner
Python string inversion
Python beginner Zundokokiyoshi
[Python] Numpy memo
Store Japanese (multibyte character string) in sqlite3 of python
[Python] How to expand variables in a character string
[Python] Operation of enumerate
Python class (Python learning memo ⑦)
My python environment memo
python openCV installation (memo)
Python module (Python learning memo ④)