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

来源:互联网 发布:苹果手机数据备份 编辑:程序博客网 时间:2024/05/16 06:11
#coding=utf8print '''PythonZ中字符串被定义为引号之间的字符集合。Python支持使用成对的单引号或双引号,三引号(三个连续的单引号或者双引号)可以来包含特殊字符。使用索引运算符([])和切片运算符([:])可以得到子字符串。字符串有其特有的索引规则:第一个字符的索引是0,最后一个字符的索引是-1。加号(+)用于字符串连接运算,星号(*)用于字符串重复。'''sgQ='hello world'doubleQueto="I am ewang"threeQueto='''one two threeone add two equal three'''print "The single queto string-------->",sgQprint "The double queto string-------->",doubleQuetoprint "The three queto string-------->",threeQuetoprint "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+","+doubleQuetoprint "%s + , + %s = %s" %(sgQ,doubleQueto,stringAdd)stringStar=sgQ * 5print "the string uses the star operator:" ,stringStar

0 0
原创粉丝点击