本文摘自php中文网,作者零到壹度,侵删。
python字符串是一种元素为字符的序列类型。因为序列类型是元素被顺序放置的一种数据结构,因此可以通过索引来获取某一个字符,或者指定索引范围来获取一组字符。1 2 3 | >>> ch= 'abcde'
>>> print ( "ch[0]=" ,ch[0], "ch[-1]=" ,ch[-1])
ch[0]= a ch[-1]= e
|
index是整数,不能越界,从0到lne(str)-1,否则出现错误。
1 2 3 4 5 6 7 | >>> len(ch)
5
>>> ch[5]
Traceback (most recent call last):
File "<pyshell#4>" , line 1, in <module>
ch[5]
IndexError: string index out of range
|
逆序打印字符串
1 2 3 4 5 6 | def resstr(ch):
mid=len(ch)
for c in range(mid):
print (ch[mid-1-c], end = '' )
>>> resstr( 'abcde' )
edcba
|
sb的准备,逆序字符串了,string是不能修改的。报错提示:'str' object does not support item assignment
字符串的分片,就是从给定的字符串分离出部分字符串,可以采用以下形式索引 i,j,k
i是起始位置,j是索引结束位置,但是不包括j位置上的字符串,索引编号每次增加的步长为k
1 2 3 4 5 | >>> s= "hello world"
>>> print (s[0:len(s):2])
hlowrd
>>> print (s[1:len(s):3], end = '---' )
eood---
|
索引index,是从0到len(str)-1,也可以使用负索引,范围是-n到-1.负索引的起始位置是字符串的结束。
1 2 3 | st= 'asdfg'
print (st[-1:0:-1])
gfds
|
字符串分片的索引,索引的起始位置i,索引结束位置j,和步长k均可以省略,省略i时,从0或者-1开始,省略j时到最后一个字符串结束,省略k时候步长为1。
1 2 3 4 5 6 7 | st= 'asdfghjk'
print (st[:0:-1])
print (st[2::2])
print (st[0:5:])
kjhgfds
dgj
asdfg
|
字符串相关的操作,可以进行连接操作,逻辑操作,还有字符串处理函数。
1 2 3 4 5 6 | st1= 'abc'
st2= "def"
print ( "{0}+{1}={2}" .format(st1,st2,st1+st2))
print ( "{0:s}*5={1}" .format(st1,st1*5))
abc+def=abcdef
abc*5=abcabcabcabcabc
|
字符串的方法
字符串是不可变的,任何字符串改变字符串后,都会返回一个新的字符串,python字符串string可以看成一个类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | st1= 'abcDEF'
print ( "{0:s}.upper()={1:s}" .format(st1,st1.upper()))
print ( "{0:s}.lower()={1:s}" .format(st1,st1.lower()))
print ( "{0:s}.swapcase()={1:s}" .format(st1,st1.swapcase()))
abcDEF.upper()=ABCDEF
abcDEF.lower()=abcdef
abcDEF.swapcase()=ABCdef
st1= 'abcDEFasde'
print ( "a count={0}" .format(st1. count ( 'a' )))
print ( "{0} start with {1} is {2} " .format(st1, 'abc' ,st1.startswith( 'abc' )))
print ( "{0} end with {1} is {2} " .format(st1, 'de' ,st1.startswith( 'de' )))
a count =2
abcDEFasde start with abc is True
abcDEFasde end with de is False
st1= ' abcDEFasde'
print ( "{0} replace 123 :{1}" .format(st1,st1.replace( 'abc' , '123' )))
print ( "{0} remove {1} left char---{2}" .format(st1, ' ' ,st1.strip()))
abcDEFasde replace 123 : 123DEFasde
abcDEFasde remove left char---abcDEFasde
|
字符串的方法太多,不在说了,没有意思。
byte对象
在Python中,byte和字符串不同,由一系列不可改变的unicode字符组成的序列叫字符串。由一系列不可改变编码介于0--255之间的字符串组成的序列称为byte对象。
1 2 3 4 5 | by=b 'abc &'
print (type(by))
print ( "length=" ,len(by))
< class 'bytes' >
length= 5
|
在字符串前面加'b'可以定义byte对象,每一个字符串可以是ascii字符等,可以使用len()函数计算byte对象的长度。
1 2 3 4 5 6 7 8 9 10 | ch=input( '输入几个数字逗号隔开:' )
d=ch.split( ',' )
print (d)
sum=0
for num in d:
sum+=float(num)
print ( "ths sum=" ,sum)
输入几个数字逗号隔开:2.2,3.3,5.5,6.8
[ '2.2' , '3.3' , '5.5' , '6.8' ]
ths sum= 17.8
|
相关推荐:
python字符串实际应用
Python3字符串各种内置函数详解
python字符串分隔
python的字符串和列表
以上就是浅谈python字符串的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
会java再去学Python容易吗
Python面向对象的知识总结
使用jupyter notebook 学习 Python
学Python能做什么的
学Python安装什么软件
Python文件存储路径如何使用变量
Python下载要钱吗
嵌入式与Python选哪个
如何用Python画简单的动物
Python程序如何快速缩进多行代码
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 浅谈python字符串