Python字符串
字符串是 Python 中最常用的数据类型。我们可以使用引号(‘或”)来创建字符串。
创建字符串很简单,只要为变量分配一个值即可。例如:
str1 = 'Hello ShowMeAI!'
str2 = "Python ShowMeAI"
Python访问字符串中的值
Python 不支持单字符类型,单字符在 Python 中也是作为一个字符串使用。Python 访问子字符串,可以使用方括号配合索引来进行字符串切片。
str1 = 'Hello World!'
str2 = "Python ShowMeAI"
print("str1[0]: ", str1[0]) #index为0的字符
print("str2[1:5]: ", str2[1:5]) #从index为1开始到5结束(不包含5)
以上实例执行结果:
str1[0]: H
str2[1:5]: ytho