Python join 連接字串用法與範例

本篇 ShengYu 介紹 Python join 連接字串的用法與範例。
以下範例是在 Python 3 環境下測試過。

使用範例

Python 中的 join 是用指定的符號來連接多個字串,跟 split 相反,
split 是根據指定的分隔符號去分割字串,
join 是根據指定的連結符號去連接字串,

程式碼如下:

python-os-path-basename.py
1
2
3
4
5
6
7
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

str1 = '-'
list1 = ['a', 'b', 'c']
str2 = str1.join(list1)
print(str2)

輸出結果如下:

1
a-b-c

下一篇介紹 split 字串分割的用法

參考
Python join()方法 | 菜鸟教程
https://www.runoob.com/python/att-string-join.html
Python3 join()方法 | 菜鸟教程
https://www.runoob.com/python3/python3-string-join.html

其它相關文章推薦
如果你想學習 Python 相關技術,可以參考看看下面的文章,
Python str 字串用法與範例
Python 新手入門教學懶人包
Python 字串分割 split
Python 去除空白與去除特殊字元 strip
Python 取代字元或取代字串 replace
Python 取出檔案名稱 basename
Python 取出目錄的路徑 dirname