字符串操作

>>> a=123
>>> id(a)   #查看a的地址
27711568
>>> a=234
>>> id(a)
27712888
>>> b=234
>>> id(b) #这里是一样的
27712888
=============================================================================
>>> str1='asdf'
>>> str1[1] #通过索引取某个元素,索引可以是负数
's'
>>> str1[1:3] #切片,记住不包含最后一个索引
'sd'
>>> str1[-1]
'f'
=============================================================================
>>> str1='asdfg'
>>> str1[1:4:1] #地一个冒号表示切片,第二个冒好表示步长
'sdf'
>>>
>>> len(str1) #表示字符串长度
5
=============================================================================
>>> str1
'asdfg'
>>> str2="lkj"
>>> str1+str2 #字符串相加
'asdfglkj'
>>> 
=============================================================================
>>> str1
'asdfg'
>>> str1*5 #字符串重复几次
'asdfgasdfgasdfgasdfgasdfg'
=============================================================================
>>> str1
'asdfg'
>>> 'c' in str1 #查看字符是否在字符串中
False
>>> 'a' in str1
True
=============================================================================
>>> str2=1245
>>> str2
1245
>>> max(str2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> str2='1245'
>>> max(str2) #取最大值,操作数是字符串
'5'
>>> min(str2)
'1'
=============================================================================
>>> str1='12345' #字符串比较
>>> str2='asdfg'
>>> cmp(str1,str2)
-1
>>> str2='asdf'
>>> cmp(str1,str2)
-1
>>> str2='a'
>>> cmp(str1,str2)
-1
>>> str2="2345"
>>> cmp(str1,str2)
-1
>>> str1
'12345'
>>> str2
'2345'
>>> cmp(str1,str2)
-1
>>> str1='1234'
>>> str2='1231'
>>> cmp(str1,str2)
1
>>> str2='1234'
>>> cmp(str1,str2)
0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值