python字符串截取
来源:互联网 发布:工业电气绘图软件 编辑:程序博客网 时间:2024/05/28 17:05
str = "0123456789"
print str[0:3] #截取第一位到第三位的字符
012
.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"))
print str[0:3] #截取第一位到第三位的字符
012
.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"))
0 0
- python截取中文字符串
- python截取中文字符串
- python 字符串截取
- Python 截取字符串
- python 字符串截取
- python截取字符串操作
- python字符串截取
- python字符串截取
- python 字符串截取
- Python字符串截取
- Python截取字符串
- python截取字符串
- python 字符串截取
- python字符串截取操作
- Python 中文字符串的截取
- python中文字符串截取问题
- python字符串的截取方法
- 怎么写python字符串截取
- 黑马程序员_OC中对象的复制
- UVA 10305 - Ordering Tasks 很简单的拓扑排序题目。
- sqlloader的基本用法例子
- ASIHTTPRequest系列(二):文件下载
- 拷贝构造函数(深拷贝和浅拷贝)
- python字符串截取
- IF大于7的经验之谈
- Ubuntu 下创建deb安装包及离线安装包制作。
- 黑马程序员_Map集合
- vim中表示当前目录和当前文件名的方法
- ASIHTTPRequest系列(一):同步和异步请求
- [屌丝PM]设计心理学——科技超载
- 算法时间复杂度分析基础
- 我的智能家居