Suzy云
下决心容易,做起来难,坚持做难上加难.. 是否真的那么难?不试试怎么知道?

本篇只是拿一段代码来对python中的字符串的一些使用做解释,来让大家更加了解python

Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AMD64)] on win32

 

Type "copyright", "credits" or "license()" for more information.

 

>>> pystr='python'    #定义变量pystr

 

>>> iscool='is cool'      #定义变量iscool

 

>>> pystr[0]       #获取pystr中0位置的字符

 

'p'     #打印出来是P

 

>>> pystr[:]    #[:]用于得到子字符串,不加限制就是全部

 

'python'

 

>>> pystr[2:5]     #获取位置在>=2,且<5的字符串内容.2<=字符串<5

 

'tho'                         #输出结果

 

>>> pystr[3:]              #3<=字符串<结束的字符串

 

'hon'                #输出结果

 

>>> pystr[1:3]      #1<=字符串<3

 

'yt'

 

>>> iscool[:2]              #0<=字符串<2

 

'is'                

 

>>> pystr[-1]                #[-1]获取最后一位的字符

 

'n'

 

>>> pystr+iscool                 #两个字符串的变量相加

 

'pythonis cool'

 

>>> pystr+' '+iscool       #两个字符串的变量相加,可以加上空格,以显得更加好看

 

'python is cool'

 

>>> pystr*2                      #两个字符串的变量相乘,就是字符串的重复输出

 

 

'pythonpython'

 

>>> '_'*80

 

'________________________________________________________________________________'    #输出直线

 

>>>

 

>>> pystr='''python....is cool'''    #字符串可以用单引号,双引号或者三个单引号进行诠释

 

>>> pystr

 

'python....is cool'

 

>>> print(pystr)     #打印出即输出字符串的变量

 

python....is cool

 

>>>

posted on 2015-01-31 18:28  Suzy云  阅读(695)  评论(0编辑  收藏  举报