Python之字符串小代码解析

本篇只是拿一段代码来对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

 

>>>

转载于:https://www.cnblogs.com/szy123618/p/4264452.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值