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

2024-05-16 21:13:25 发布

您现在位置:Python中文网/ 问答频道 /正文

为什么xy在下面的代码中没有输出?仅当子字符串小于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)

Tags: the字符串代码inyouresultcanprint
2条回答
x = c[0] #puts “k” in x
y = c[2] #puts “a” in y

如果按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

相关问题 更多 >