您的位置:首页 > 编程语言 > Python开发

python中字符串定义、索引、切片、加号、星号操作实例

2017-02-10 11:11 746 查看
#coding=utf8
print '''
PythonZ中字符串被定义为引号之间的字符集合。
Python支持使用成对的单引号或双引号,三引号(三个连续的单引号或者双引号)可以来包含特殊字符。
使用索引运算符([])和切片运算符([:])可以得到子字符串。
字符串有其特有的索引规则:第一个字符的索引是0,最后一个字符的索引是-1。
加号(+)用于字符串连接运算,星号(*)用于字符串重复。
'''
sgQ='hello world'
doubleQueto="I am ewang"
threeQueto='''
one two three
one add two equal three
'''
print "The single queto string-------->",sgQ
print "The double queto string-------->",doubleQueto
print "The three queto string-------->",threeQueto

print "The use of index------------->",sgQ[0],sgQ[1],sgQ[2],sgQ[-1]
print "The use of slice------------->",doubleQueto[:8],doubleQueto[:-1],doubleQueto[3:],doubleQueto[0:5]

stringAdd=sgQ+","+doubleQueto
print "%s + , + %s = %s" %(sgQ,doubleQueto,stringAdd)

stringStar=sgQ * 5
print "the string uses the star operator:" ,stringStar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐