本文实例讲述了python开发之字符串string操作方法。分享给大家供大家参考,具体如下:
在python中,对于字符串string的操作,我们有必要了解一下,这样在我们的以后的开发中会给我们带来很多方便
下面是我学习的笔记:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#python-string
#python中的字符串用单引号''和双引号""标示
strA = 'this is a string'
strB = "this is a message!"
#打印两个字符串
print ( "打印两个字符串" )
print ( 'strA = ' + strA)
print ( 'strB = ' + strB)
print ( "#############################" )
strC = 'I don\'t know anything'
strD = '\'Yes\',I know.'
print ( "字符串中的转义字符" )
print ( 'strA = ' + strC)
print ( 'strB = ' + strD)
print ( "#############################" )
strE = '这是我的blog,欢迎大家来\n我的博客溜达'
print ( "字符串中的换行" )
print ( 'strA = ' + strE)
print ( "#############################" )
strF = 'this is ' 'message'
strG = 'Hongten'
strH = strG * 3
print ( '字符串可以用\'+\'号连接(或者说粘合),也可以用\'*\'号循环' )
print ( 'strF原有形式为:\'this is \'\'message\'' )
print ( '粘合后的strF:' + strF)
print ( 'strG原值为:\'Hongten\',strH = strG * 3,此时strH为:' + strH)
print ( "#############################" )
strI = 'hongtenzone@foxmail.com'
print ( '字符串可以使用下标(索引)查询' )
print ( '源字符串strI = \'hongtenzone@foxmail.com\'' )
print ( '字符串strI的长度,len(strI) = ' )
print ( len (strI))
print ( 'strI[0] = ' + strI[ 0 ])
print ( 'strI[10] = ' + strI[ 10 ])
print ( 'strI[-1] = strI[len(strI) - 1]' )
print ( 'strI[-1] = ' + strI[ - 1 ])
print ( 'strI[len(strI) - 1] = ' + strI[ len (strI) - 1 ])
print ( "#############################" )
print ( 'Python 字符串不能改写。按字符串索引赋值会产生错误:' )
print ( 'strI[0] = \'x\',这样就会产生错误啦' )
print ( "#############################" )
print ( '过大的索引代替为字符串大小,下界比上界大的返回空字符串' )
print ( 'strI[0:100] = ' + strI[ 0 : 100 ])
print ( "#############################" )
print ( '索引可以是负数,计数从右边开始' )
print ( 'strI[-2] = ' + strI[ - 2 ])
print ( 'strI[-23:] = ' + strI[ - 23 :])
print ( "#############################" )
print ( '不过-0 还是0,所以它不是从右边计数的!' )
print ( 'strI[0] = ' + strI[ 0 ])
print ( 'strI[-0] = ' + strI[ - 0 ])
|
运行效果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
Python 2.7 . 9 (default, Dec 10 2014 , 12 : 24 : 55 ) [MSC v. 1500 32 bit (Intel)] on win32
Type "copyright" , "credits" or "license()" for more information.
>>> = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = RESTART = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
>>>
打印两个字符串
strA = this is a string
strB = this is a message!
#############################
字符串中的转移字符
strA = I don't know anything
strB = 'Yes' ,I know.
#############################
字符串中的换行
strA = 这是我的blog,欢迎大家来
我的博客溜达
#############################
字符串可以用 '+' 号连接(或者说粘合),也可以用 '*' 号循环
strF原有形式为: 'this is ' 'message'
粘合后的strF:this is message
strG原值为: 'Hongten' ,strH = strG * 3 ,此时strH为:HongtenHongtenHongten
#############################
字符串可以使用下标(索引)查询
源字符串strI = 'hongtenzone@foxmail.com'
字符串strI的长度, len (strI) =
23
strI[ 0 ] = h
strI[ 10 ] = e
strI[ - 1 ] = strI[ len (strI) - 1 ]
strI[ - 1 ] = m
strI[ len (strI) - 1 ] = m
#############################
Python 字符串不能改写。按字符串索引赋值会产生错误:
strI[ 0 ] = 'x' ,这样就会产生错误啦
#############################
过大的索引代替为字符串大小,下界比上界大的返回空字符串
strI[ 0 : 100 ] = hongtenzone@foxmail.com
#############################
索引可以是负数,计数从右边开始
strI[ - 2 ] = o
strI[ - 23 :] = hongtenzone@foxmail.com
#############################
不过 - 0 还是 0 ,所以它不是从右边计数的!
strI[ 0 ] = h
strI[ - 0 ] = h
>>>
|
print打印字符串语句如下:
1
2
3
4
5
6
7
8
|
print ( '理解切片的最好方式是把索引视为两个字符之间的点,第一个字符的左边是0,字符串中第n个字符的右边是索引n' )
print ( ' +---+---+---+---+---+ ' )
print ( ' | H | e | l | p | A |' )
print ( ' +---+---+---+---+---+ ' )
print ( ' 0 1 2 3 4 5 ' )
print ( '-5 -4 -3 -2 -1 -0' )
print ( '第一行是字符串中给定的0到5各个索引的位置,第二行是对应的负索引。从i 到j 的切片由这两个标志之间的字符组成' )
print ( '对于非负索引,切片长度就是两索引的差。例如,word[1:3] 的长度是2' )
|
运行效果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Python 2.7 . 9 (default, Dec 10 2014 , 12 : 24 : 55 ) [MSC v. 1500 32 bit (Intel)] on win32
Type "copyright" , "credits" or "license()" for more information.
>>> = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = RESTART = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
>>>
理解切片的最好方式是把索引视为两个字符之间的点,第一个字符的左边是 0 ,字符串中第n个字符的右边是索引n
+ - - - + - - - + - - - + - - - + - - - +
| H | e | l | p | A |
+ - - - + - - - + - - - + - - - + - - - +
0 1 2 3 4 5
- 5 - 4 - 3 - 2 - 1 - 0
第一行是字符串中给定的 0 到 5 各个索引的位置,第二行是对应的负索引。从i 到j 的切片由这两个标志之间的字符组成
对于非负索引,切片长度就是两索引的差。例如,word[ 1 : 3 ] 的长度是 2
>>>
|
希望本文所述对大家Python程序设计有所帮助。