s="abcde"
s[0]
s[-1]
s[-5]
s[:-1] #去掉最后一个字符,比如换行符
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
切片访问越界会得到一个空集。无需做访问控制。
对于单个字符时无法赋值的,因为字符串是不可变的。如果需要一个不同的字符串,那就creat一个新的字符串吧,使用切片能够很容易达到这点。
内置函数len返回字符串的长度。
用encode和decode来问字符串编码解码。(关于编码类型的问题,需要专门开一个文件来讨论)
常用方法:
startswith 检测开头
示例:
1. a = 'leonis'
if a.startswith('le'):
print ‘le'
endswith 检测结尾
示例:
1. a = ‘leonis'
if a.endswith(‘is'):
print ‘is'
in 检测是否为其中一部分
示例:
1. a = ‘leonis'
if ‘o' in a:
print ‘a'
find 查询包含
示例:
本站所有资源全部来源于网络,若本站发布的内容侵害到您的隐私或者利益,请联系我们删除!
合作方式
Copyright © 2004-2018 https://www.gxlcms.com/. All Rights Reserved.
豫ICP备19030742号