python中的幾個字串處理函式

2021-09-13 23:48:21 字數 1739 閱讀 9296

功能:將字串str當成有效的表示式來求值並返回計算結果。

語法:eval(source[, globals[, locals]]) -> value

引數:

>**例子:**

1可以把list,tuple,dict和string相互轉化。

2*************************===

3 字串轉換成列表

4 >>>a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"

5 >>>type(a)

6 7 >>> b = eval(a)

8 >>> print b

9 [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]

10 >>> type(b)

11 12 *************************===

13 字串轉換成字典

14 >>> a = ""

15 >>> type(a)

16 17 >>> b = eval(a)

18 >>> print b

19 20 >>> type(b)

21 22 *************************===

23 字串轉換成元組

24 >>> a = "([1,2], [3,4], [5,6], [7,8], (9,0))"

25 >>> type(a)

26 27 >>> b = eval(a)

28 >>> print b

29 ([1, 2], [3, 4], [5, 6], [7, 8], (9, 0))

30 >>> type(b)

31

功能:通過指定分隔符對字串進行切片,如果引數num 有指定值,則分隔為 num 個子字串。並返回分割後的字串列表。

語法:str.split(str="", num=string.count(str))

引數:

例子:`str = "line1-abcdef nline2-abc nline4-abcd";

print str.split( );

print str.split(' ', 1 );`

輸出:

`['line1-abcdef', 'line2-abc', 'line4-abcd']

['line1-abcdef', 'nline2-abc nline4-abcd']`

功能:連線字串陣列。將字串、元組、列表中的元素以指定的字元(分隔符)連線生成乙個新的字串

語法:'sep'.join(seq)

引數:

例子:

幾個字串的處理函式

將字串中的小寫字母轉換為大寫 str 要轉換的字串 len 字串長度 void lowertocap u8 str,u8 len 對比字串str1和str2 str1 字串1指標 str2 字串2指標 返回值 0,相等 1,不相等 u8 usmart strcmp u8 str1,u8 str2 r...

Python裡的幾個字串處理函式

功能 將字串str當成有效的表示式來求值並返回計算結果。語法 eval source globals locals value 引數 例子 1可以把list,tuple,dict和string相互轉化。2 3 字串轉換成列表 4 a 1,2 3,4 5,6 7,8 9,0 5 type a 6 7 ...

寫幾個字串演算法

1 實現strstr函式 函式原型是char strstr char str1,char str2 作用是找出str2字串在str1字串中第一次出現的位置 不包括str2的串結束符 如果找到返回該位置的指標。若找不到,返回null指標。2 實現strcpy函式 char strcpy char de...