Python 如果长度小于3,子字符串将不会输出任何内容

Python 如果长度小于3,子字符串将不会输出任何内容,python,Python,为什么x和y在下面的代码中没有输出?它仅在子字符串小于0:3时发生 textString = "You can milk a yak in London Zoo" print(textString) a = len(textString) #puts a = 32 b = textString.index('milk') #puts 8 in b c = textString[11:17] #puts "k a yak" in c # You could

为什么x和y在下面的代码中没有输出?它仅在子字符串小于0:3时发生

textString = "You can milk a yak in London Zoo"
print(textString)
a = len(textString) #puts a = 32
b = textString.index('milk') #puts 8 in b
c = textString[11:17] #puts "k a yak" in c
# You could find the positions of the spaces in c
# but this solution assumes they are known
x = c[0:0] #puts “k” in x
y = c[2:2] #puts “a” in y
z = c[4:6] #puts “yak” in z

result = x+y+z
print(x)
print(y)
print(z)
print(result)

如果按1关闭索引,则第二个数组索引器“最多但不包括”

textString = "You can milk a yak in London Zoo"
print(textString)
a = len(textString) #puts a = 32
b = textString.index('milk') #puts 8 in b
c = textString[11:18] #puts "k a yak" in c
# You could find the positions of the spaces in c
# but this solution assumes they are known
x = c[0:1] #puts “k” in x
y = c[2:3] #puts “a” in y
z = c[4:7] #puts “yak” in z

result = x+y+z
print(x)
print(y)
print(z)
print(result) # -> kayak

您期望的输出是什么?切片时,第二个索引是独占的,而不是包含的。c[0:0]表示从索引0开始的任何字符,其索引小于0,与c[2:2]相同。在这两种情况下,答案都是空字符串。从c开始,预期输出为Kayak,您的任何假设都不成立。如果您想独占使用子字符串,可以使用c[0:1]和c[2:3]。无论哪种方式,您当前获得的输出都是kaya,因为出于与上面相同的原因,c[4:6]只获得两个字符,应该替换为c[4:7]或c[4:]
textString = "You can milk a yak in London Zoo"
print(textString)
a = len(textString) #puts a = 32
b = textString.index('milk') #puts 8 in b
c = textString[11:18] #puts "k a yak" in c
# You could find the positions of the spaces in c
# but this solution assumes they are known
x = c[0:1] #puts “k” in x
y = c[2:3] #puts “a” in y
z = c[4:7] #puts “yak” in z

result = x+y+z
print(x)
print(y)
print(z)
print(result) # -> kayak