Think Twice
IT技術メモ | Pythonのメモ
Created: 2021-10-18 / Updated: 2021-10-18

Pythonでリスト中の文字列を結合して文字列を作る


目次


str.joinメソッド

strjoinメソッドを使います。

Copy
>>> list = ["a", "b", "c"]
>>> "/".join(list)
'a/b/c'

ただし、リストの中身が文字列じゃないとエラーになるので注意して下さい。

TypeError
Copy
>>> list2 = [100, 200, 300]
>>> "/".join(list2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected str instance, int found


参考

参照

参考サイト