【问题标题】:How to get full word from array instead of first letter in python如何从数组中获取完整的单词而不是python中的第一个字母
【发布时间】:2019-02-02 03:12:55
【问题描述】:

所以我有这个代码:

search_init = 'the string'
search = search_init.replace(' ', '+')
search_array = search.strip("+")
print search_array[1]

我希望在控制台的打印中得到“字符串”。但相反,我得到“h”。我该如何解决这个问题?我正在尝试将“字符串”拆分为一个数组,然后从数组中提取单个单词。 (抱歉新手问题哈哈)

【问题讨论】:

  • 当你真的想使用.split('+')时,也许你只是在使用.strip('+')
  • 我认为您混淆了 strip()split()。不需要replace>。

标签: python arrays string


【解决方案1】:

您根据空间执行split,然后从索引 1 中提取。

search_init = 'the string'
search = search_init.split(" ")
print search[1]

【讨论】:

  • 天哪,我为那个感到愚蠢。当 stackoverflow 允许我时,将在 5 分钟内将此标记为解决方案。谢谢!!!
  • 很高兴它有帮助!
猜你喜欢
  • 2017-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-18
  • 2023-03-30
  • 2013-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多