在python中,字符串是一种最常用的数据类型。在创建字符串的时候,我们可以使用引号( ' 或 " )来创建。
创建字符串其实一点都不难,只要给变量分配一个值就可以了。
实例:
a = 'Durant went to the sun!'
b = "Irving goes to the lone ranger!"
Python访问字符串中的值
在Python中,是不支持单字符类型的,单字符在Python中是作为一个字符串使用的。
Python访问子字符串,可以使用方括号 [ ] 来截取字符串,字符串的截取的语法格式如下:
变量[头下标:尾下标]
索引值是以 0 为开始值,-1 为从末尾的开始位置。
实例:
a = 'Durant went to the sun!'
b = "Irving goes to the lone ranger!"
print ("a[1]: ", a[1])
print ("b[2:9]: ", b[2:9])
执行结果为: