# 此段代码用于演示字符串切片和比较
string = 'hello word' # 定义一个字符串变量
a = string[0:4] # 从字符串中切出从索引0开始到索引4(不包括4)的子字符串
b = string[0:4] # 做同样的切片操作,得到相同的子字符串
print(a) # 打印子字符串a
print(b) # 打印子字符串b
print(a is b) # 比较两个子字符串是否为同一个对象
输出结果为:
hell
hell
False
答案:切片赋值为赋值两遍,答案为False。变更为b = a ,或者a和b直接赋值hell,则为True
评论前必须登录!
注册