python join字元連線函式的使用方法

2021-06-20 18:26:26 字數 1572 閱讀 9206

string模組的join方法,因此它需要兩個引數,乙個是list,另乙個是分隔符。而且呼叫時正如cnxo所示,如果你匯入了 string模組:

import string

你需要使用string.join()這樣來用。

但現在string物件本身就有這樣方法,這樣它只需要乙個分隔符引數。用法為:

'+'.join(list1)

最簡單的我們用到了join函式,例項

print ''.join(list('hello'))

把資料連線

word1 = "a"

word2 = "few"

word3 = "good"

word4 = "words"

wordlist = ["a", "few", "more", "good", "words"]

print "words:" + word1 + word2 + word3 + word4

print "list: " + ' '.join(wordlist)

例項

word1 = "a"

word2 = "few"

word3 = "good"

word4 = "words"

wordlist = ["a", "few", "more", "good", "words"]

#joining a list of words

sentence = "second:"

for word in wordlist:

sentence += " " + word

sentence += "."

print sentence

綜合上面所述看個例項

# splitting strings

string1 = "a, b, c, d, e, f"

print "string is:", string1

print "split string by spaces:", string1.split()

print "split string by commas:", string1.split( "," )

print "split string by commas, max 2:", string1.split( ",", 2 )

print

# joining strings

list1 =  [ "a", "b", "c", "d", "e", "f" ]

string2 = "___"

print "list is:", list1

print 'joining with "%s": %s' 

% ( string2, string2.join ( list1 ) )

print 'joining with "-.-":', "-.-".join( list1 )

**:

c isdigit函式 Arduino 字元函式

所有資料都以字元形式輸入計算機,包括字母,數字和各種特殊符號。在本章節中,我們討論c 檢查和操作單個字元的功能。字元處理庫包括幾個函式,執行有用的測試和字元資料的操作。每個函式接收乙個字元,表示為int或eof作為引數。字元通常作為整數操作。記住,eof通常具有值 1,而一些硬體架構不允許負值儲存在...

oracle function 分割字串函式

這個函式要用到,記錄下 create or replace function split src varchar2,delimiter varchar2 src 分割的字串,delimiter 分隔符 return t str20list is psrc varchar2 500 a t str20...

unicode字元大全 python內建函式大全1

1 abs 返回數字的絕對值。2 all 如果集合中所有元素是true或集合為空集合,返回true。3 any 如果集合中有一項元素是true,返回true 空集合為false 4 ascii 返回乙個表示物件的字串。5 bin 將整數轉換為字首為 0b 的二進位制字串。6 bool 返回乙個布林值...