合併字串的5種方法 python 面試

2021-10-06 15:50:29 字數 819 閱讀 9011

方法一: 使用join的方法

>>

>

" ".join(

["a","b","c","d"])

'a b c d'

方法二: 使用字串格式化拼接

>>

>

"%s's age is %d" % (

"jerry", 18)

"jerry's age is 18"

>>

>

方法三: 使用+來拼接

>>

>

"<<" + name1 + " & " + name2 + ">>"

'<>'

>>

>

方法四: 使用for迴圈來處理

>>

> long_string =

"">>

>

for s in

["a", "b", "c", "d"

]:... long_string += s

...>>

> print(long_string)

abcd

>>

>

方法五: 使用operator中的add函式結合reduce來處理

>>

>

import operator

>>

> from functools import reduce

>>

> reduce(operator.add, [

"a", "b", "c", "d"

], ""

)

MFC獲取字串長度的5種方法

char s1 中文abc wchar t s2 l 中文abc 1.sizeof 獲取字元陣列的位元組數 包括結束符0 sizeof s1 8 ansi sizeof s2 12 unicode 2.strlen wcslen 採取0作為字串的結束符,並返回不包括0在內的字元數目 strlen s...

mysql 字串 反轉 字串反轉的9種方法

1.使用array.reverse方法 對於字串反轉,我們可以使用.net類庫自帶的array.reverse方法 public static string reversebyarray string original char c original.tochararray array.revers...

Shell指令碼中計算字串長度的5種方法

有時在linux作業系統中需要計算某個字串的長度,通過查詢資料整理了下目前shell中獲取字串的長度的多種方法,在這裡分享給大家,方法如下 方法1 使用wc l命令 wc l可以獲取到當前行的長度,因此對於單獨行的字串可以用這個簡單的方法獲取,另外wc l則是獲取當前字串內容的行數。如下 echo ...