Python的字符串操作 « 邵珠庆の博客

邵珠庆の博客 生命只有一次,你可以用它来做些更多伟大的事情–Make the world a little better and easier

22十/1135

Python的字符串操作

 

1.python字符串通常有单引号('...')、双引号("...")、三引号("""...""")或 ('''...''')包围,三引 号包含的字符串可由多行组成,一般可表示大段的叙述性字符串。在使用时基本没有差别,但双引号和三引号("""...""")中可以包含单引号,三引号 ('''...''')可以包含双引号,而不需要转义。

 
2.用(\)对特殊字符转义,如(\)、(')、(")。
 
3.常用字符串函数
1)str.count() //返回该字符串中某个子串出现的次数
2)str.find()   //返回某个子串出现在该字符串的起始位置
3)str.lower() //将该字符串全部转化为小写
4)str.upper() //转为大写
5)str.split() //分割字符串,返回字串串列表,默认以空格分割
6)len(str)     //返回字符串长度
7)迭代替换指定字符串
for i, j in {'chr':'\t', ':' : '\t', '..' : '\t'}.iteritems():
    text = text.replace(i, j)
print text
 
例如:
>>> str = 'Hello, world'
>>> str.count('o')
>>> 2
>>> str.find('lo')
>>> 3
>>> str.lower()
>>> 'hello, world'
>>> str.upper()
>>> 'HELLO, WORLD'
>>> str.split()
>>> ['Hello,', 'world']
>>> str.split(',')
>>> ['Hello', ' world']
>>> len(str)
>>> 13
>>> str
>>> 'Hello, world'

1.复制字符串

#strcpy(sStr1,sStr2)
sStr1 = 'strcpy'
sStr2 = sStr1
sStr1 = 'strcpy2'
print sStr2

2.连接字符串

#strcat(sStr1,sStr2)
sStr1 = 'strcat'
sStr2 = 'append'
sStr1 += sStr2
print sStr1

3.查找字符

#strchr(sStr1,sStr2)
sStr1 = 'strchr'
sStr2 = 'r'
nPos = sStr1.index(sStr2)
print nPos

4.比较字符串

#strcmp(sStr1,sStr2)
sStr1 = 'strchr'
sStr2 = 'strch'
print cmp(sStr1,sStr2)

5.扫描字符串是否包含指定的字符

#strspn(sStr1,sStr2)
sStr1 = '12345678'
sStr2 = '456'
#sStr1 and chars both in sStr1 and sStr2
print len(sStr1 and sStr2)

6.字符串长度

#strlen(sStr1)
sStr1 = 'strlen'
print len(sStr1)

7.将字符串中的小写字符转换为大写字符

#strlwr(sStr1)
sStr1 = 'JCstrlwr'
sStr1 = sStr1.upper()
print sStr1

8.追加指定长度的字符串

#strncat(sStr1,sStr2,n)
sStr1 = '12345'
sStr2 = 'abcdef'
n = 3
sStr1 += sStr2[0:n]
print sStr1

9.字符串指定长度比较

#strncmp(sStr1,sStr2,n)
sStr1 = '12345'
sStr2 = '123bc'
n = 3
print cmp(sStr1[0:n],sStr2[0:n])

10.复制指定长度的字符

#strncpy(sStr1,sStr2,n)
sStr1 = ''
sStr2 = '12345'
n = 3
sStr1 = sStr2[0:n]
print sStr1

11.字符串比较,不区分大小写

#stricmp(sStr1,sStr2)
sStr1 = 'abcefg'
sStr2 = 'ABCEFG'
print cmp(sStr1.upper(),sStr2.upper())

12.将字符串前n个字符替换为指定的字符

#strnset(sStr1,ch,n)
sStr1 = '12345'
ch = 'r'
n = 3
sStr1 = n * ch + sStr1[3:]
print sStr1

13.扫描字符串

#strpbrk(sStr1,sStr2)
sStr1 = 'cekjgdklab'
sStr2 = 'gka'
nPos = -1
for c in sStr1:
    if c in sStr2:
        nPos = sStr1.index(c)
        break
print nPos

14.翻转字符串

#strrev(sStr1)
sStr1 = 'abcdefg'
sStr1 = sStr1[::-1]
print sStr1

15.查找字符串

python strstr

#strstr(sStr1,sStr2)
sStr1 = 'abcdefg'
sStr2 = 'cde'
print sStr1.find(sStr2)

16.分割字符串

#strtok(sStr1,sStr2)
sStr1 = 'ab,cde,fgh,ijk'
sStr2 = ','
sStr1 = sStr1[sStr1.find(sStr2) + 1:]
print sStr1

邵珠庆推荐文章

博文加载中...

anyShare分享到:

喜欢这个文章吗?

考虑订阅我们的RSS Feed吧!

发布在 邵珠庆

评论 (35) 引用 (0)
  1. Archaeologists may theorise about this and that, but it takes someone to spend a lot of time there to gain insight into how the house was used.

  2. This all said, whenever I have the energy and the opportunity to come out and meet all of

  3. This all said, whenever I have the energy and the opportunity to come out and meet all of

  4. I must admit that this is one wonderful insight. It surely gives a company the opportunity to get in on the ground floor and also really take part in creating something special and tailored to their needs.

  5. To think, I was confused a mntiue ago.

  6. 好久没来了。来看看。


Leave a comment

(required)


*

还没有引用.