python - 如果长度小于 3,子字符串不会输出任何内容
问题描述
为什么在下面的代码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)
解决方案
如果您的索引减一 - 第二个数组索引器“最多但不包括”
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
推荐阅读
- python - 读取具有两个外键列的表数据
- swift - 如何从 AVCapturePhotoOutput 中获取图像 url?
- excel - 对于一列中的每个单元格,检查下一列中的多个匹配项,然后突出显示它们
- bandwidth - 如何在 netperf 中控制比特率/带宽?相当于 iperf 中的 -b 选项?
- mongodb - 为大型电子商务项目选择 NoSql(Cassandra 或 MongoDb)和 SQL 之间的数据库类型,包括对非结构化数据模型的长文本搜索
- joomla - 以编程方式删除 joomla 用户及其数据的正确方法是什么?
- typescript - Vue.js:打字稿无法识别组件中的混合数据
- javascript - 如何将 DIRECTORY 作为输入并在 HTML 和 javascript 中一一处理其中的所有文件
- azure-language-understanding - LUIS 应用程序 - 通过端点命中数获取流行意图
- node.js - 有没有办法可以在 nodejs 和 postgresql 中解决这个问题?