字符串是由零个或多个字符组成的有限序列。而在Python 3中,它有着更明确的意思:字符串是由Unicode码点组成的不可变序列。
获取Python字符串中的某字符可以使用索引:
lang = '<a href="https://www.gaodaima.com/tag/python" title="查看更多关于python的文章" target="_blank">python</a>' <a href="https://www.gaodaima.com/tag/lang" title="查看更多关于lang的文章" target="_blank">lang</a>[0] # p lang[3] # h
www#gaodaima.com来源gao!daima.com搞$代!码网搞代码
截取字符串中的一段字符串可以使用切片,切片在方括号中使用冒号:来分隔需要截取的首尾字符串的索引,方式是包括开头,不包括结尾
lang[1:4] # yth
当尾索引没有给出时,默认截取到字符串的末尾
lang[1:] # ython
当头索引没有给出的时候默认从字符串开头开始截取
lang[:3] # pyt
当尾索引和头索引都没有给出的时候,默认返回整个字符串,不过这只是一个浅拷贝
lang[:] # python
更多技术请关注云海天Python教程。
来源:搞代码网:原文地址:https://www.gaodaima.com