s.strip() .lstrip() .rstrip(',') 去空格及特殊符號
復制字符串
Python
連接字符串
Python
查找字符
< 0 未找到
Python
比較字符串
Python
掃描字符串是否包含指定的字符
Python
1
#strspn(sStr1,sStr2)
2
sStr1
=
'12345678'
3
sStr2
=
'456'
4
#sStr1 and chars both in sStr1 and sStr2
5
print
len
(sStr1
and
sStr2)
字符串長度
Python
將字符串中的大小寫轉換
Python
追加指定長度的字符串
Python
1
#strncat(sStr1,sStr2,n)
2
sStr1
=
'12345'
3
sStr2
=
'abcdef'
4
n
=
3
5
sStr1
+
=
sStr2[
0
:n]
6
print
sStr1
字符串指定長度比較
Python
1
#strncmp(sStr1,sStr2,n)
2
sStr1
=
'12345'
3
sStr2
=
'123bc'
4
n
=
3
5
print
cmp
(sStr1[
0
:n],sStr2[
0
:n])
復制指定長度的字符
Python
將字符串前n個字符替換為指定的字符
Python
掃描字符串
Python
1
#strpbrk(sStr1,sStr2)
2
sStr1
=
'cekjgdklab'
3
sStr2
=
'gka'
4
nPos
=
-
1
5
for
c
in
sStr1:
6
????
if
c
in
sStr2:
7
????????
nPos
=
sStr1.index(c)
8
????????
break
9
print
nPos
翻轉字符串
Python
查找字符串
Python
分割字符串
Python
1
#strtok(sStr1,sStr2)
2
sStr1
=
'ab,cde,fgh,ijk'
3
sStr2
=
','
4
sStr1
=
sStr1[sStr1.find(sStr2)
+
1
:]
5
print
sStr1
6
或者
7
s
=
'ab,cde,fgh,ijk'
8
print
(s.split(
','
))
連接字符串
Python
PHP 中 addslashes 的實現
Python
1
def
addslashes(s):
2
????
d
=
{
'"'
:
'\\"'
, "
'":"\\'
", "
\
0
":"
\\\
0
", "
\\
":"
\\\\"}
3
????
return
''.join(d.get(c, c)
for
c
in
s)
4
?5
s
=
"John 'Johny' Doe (a.k.a. \"Super Joe\")\\\0"
6
print
s
7
print
addslashes(s)
只顯示字母與數字
Python
ve/2011/08/29/2158268.html