python字符串操作
来源:互联网 发布:linux 复制文件覆盖 编辑:程序博客网 时间:2024/06/01 18:53
Python 截取字符串使用 变量[头下标:尾下标],就可以截取相应的字符串,其中下标是从0开始算起,可以是正数或负数,下标可以为空表示取到头或尾。
# 例1:字符串截取str = '12345678'print str[0:1]>> 1# 输出str位置0开始到位置1以前的字符print str[1:6]>> 23456# 输出str位置1开始到位置6以前的字符num = 18str = '0000' + str(num)# 合并字符串print str[-5:]# 输出字符串右5位>> 00018
Python 替换字符串使用 变量.replace("被替换的内容","替换后的内容"[,次数]),替换次数可以为空,即表示替换所有。要注意的是使用replace替换字符串后仅为临时变量,需重新赋值才能保存。
# 例2:字符串替换str = 'akakak'str = str.replace('k',' 8')# 将字符串里的k全部替换为8print str>> 'a8a8a8'# 输出结果
Python 查找字符串使用 变量.find("要查找的内容"[,开始位置,结束位置]),开始位置和结束位置,表示要查找的范围,为空则表示查找所有。查找到后会返回位置,位置从0开始算,如果每找到则返回-1。
# 例3:字符串查找str = 'a,hello'print str.find('hello')# 在字符串str里查找字符串hello>> 2# 输出结果
Python 分割字符串使用 变量.split("分割标示符号"[分割次数]),分割次数表示分割最大次数,为空则分割所有。
例4:字符分割str = 'a,b,c,d'strlist = str.split(',')# 用逗号分割str字符串,并保存到列表for value in strlist:# 循环输出列表值 print value>> a# 输出结果>> b>> c>> d
s.strip() .lstrip() .rstrip(',') 去空格及特殊符号
复制字符串
Python
1
#strcpy(sStr1,sStr2)
2
sStr1
=
'strcpy'
3
sStr2
=
sStr1
4
sStr1
=
'strcpy2'
5
print
sStr2
连接字符串
Python
1
#strcat(sStr1,sStr2)
2
sStr1
=
'strcat'
3
sStr2
=
'append'
4
sStr1
+
=
sStr2
5
print
sStr1
查找字符
< 0 未找到
Python
1
#strchr(sStr1,sStr2)
2
sStr1
=
'strchr'
3
sStr2
=
's'
4
nPos
=
sStr1.index(sStr2)
5
print
nPos
比较字符串
Python
1
#strcmp(sStr1,sStr2)
2
sStr1
=
'strchr'
3
sStr2
=
'strch'
4
print
cmp
(sStr1,sStr2)
扫描字符串是否包含指定的字符
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
1
#strlen(sStr1)
2
sStr1
=
'strlen'
3
print
len
(sStr1)
将字符串中的大小写转换
Python
1
#strlwr(sStr1)
2
sStr1
=
'JCstrlwr'
3
sStr1
=
sStr1.upper()
4
#sStr1 = sStr1.lower()
5
print
sStr1
追加指定长度的字符串
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
1
#strncpy(sStr1,sStr2,n)
2
sStr1
=
''
3
sStr2
=
'12345'
4
n
=
3
5
sStr1
=
sStr2[
0
:n]
6
print
sStr1
将字符串前n个字符替换为指定的字符
Python
1
#strnset(sStr1,ch,n)
2
sStr1
=
'12345'
3
ch
=
'r'
4
n
=
3
5
sStr1
=
n
*
ch
+
sStr1[
3
:]
6
print
sStr1
扫描字符串
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
1
#strrev(sStr1)
2
sStr1
=
'abcdefg'
3
sStr1
=
sStr1[::
-
1
]
4
print
sStr1
查找字符串
Python
1
#strstr(sStr1,sStr2)
2
sStr1
=
'abcdefg'
3
sStr2
=
'cde'
4
print
sStr1.find(sStr2)
分割字符串
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
1
delimiter
=
','
2
mylist
=
[
'Brazil'
,
'Russia'
,
'India'
,
'China'
]
3
print
delimiter.join(mylist)
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
1
def
OnlyCharNum(s,oth
=
''):
2
s2
=
s.lower();
3
fomart
=
'abcdefghijklmnopqrstuvwxyz0123456789'
4
for
c
in
s2:
5
if
not
c
in
fomart:
6
s
=
s.replace(c,'');
7
return
s;
8
9
print
(OnlyStr(
"a000 aa-b"
))
Python 截取字符串使用 变量[头下标:尾下标],就可以截取相应的字符串,其中下标是从0开始算起,可以是正数或负数,下标可以为空表示取到头或尾。
# 例1:字符串截取str = '12345678'print str[0:1]>> 1# 输出str位置0开始到位置1以前的字符print str[1:6]>> 23456# 输出str位置1开始到位置6以前的字符num = 18str = '0000' + str(num)# 合并字符串print str[-5:]# 输出字符串右5位>> 00018
Python 替换字符串使用 变量.replace("被替换的内容","替换后的内容"[,次数]),替换次数可以为空,即表示替换所有。要注意的是使用replace替换字符串后仅为临时变量,需重新赋值才能保存。
# 例2:字符串替换str = 'akakak'str = str.replace('k',' 8')# 将字符串里的k全部替换为8print str>> 'a8a8a8'# 输出结果
Python 查找字符串使用 变量.find("要查找的内容"[,开始位置,结束位置]),开始位置和结束位置,表示要查找的范围,为空则表示查找所有。查找到后会返回位置,位置从0开始算,如果每找到则返回-1。
# 例3:字符串查找str = 'a,hello'print str.find('hello')# 在字符串str里查找字符串hello>> 2# 输出结果
Python 分割字符串使用 变量.split("分割标示符号"[分割次数]),分割次数表示分割最大次数,为空则分割所有。
例4:字符分割str = 'a,b,c,d'strlist = str.split(',')# 用逗号分割str字符串,并保存到列表for value in strlist:# 循环输出列表值 print value>> a# 输出结果>> b>> c>> d
字符串里有三个去空格的函数strip 同时去掉左右两边的空格lstrip 去掉左边的空格rstrip 去掉右边的空格
- Python字符串操作
- Python字符串操作
- Python字符串操作
- python 字符串操作
- python字符串操作
- Python字符串操作
- Python字符串操作[转]
- Python字符串操作
- Python字符串操作
- Python学习---字符串操作
- python 中的字符串操作
- python------字符串连接操作
- python字符串操作
- python 字符串操作
- Python字符串操作
- python字符串操作
- Python字符串操作
- python 中的字符串操作
- 【学习笔记】网络编程基础API
- Socket bind failed: [730048]异常的处理
- java非常有用的代码段(关键时候可以看看)
- XML、JSON数据互转 在线快速互转网址
- Django URL解析的一些注意点
- python字符串操作
- jquery validate 详解一
- win下mongodb安装
- centos6.4下部署tomcat7,搭建android-ADT
- 优酷网和56网获取网络流播放地址url的方法
- 去APP Store评分,下载
- Yahoo! s4和Twitter storm的粗略比较
- 学习JAVA应该需要掌握什么?
- 再见亦或是再见