【问题标题】:For loops python problems [closed]对于循环python问题[关闭]
【发布时间】:2011-11-12 02:15:20
【问题描述】:

编写一个函数,将一个字符串和一个数字 n 作为输入参数,并返回一个由每第 n 个字母组成的新创建的字符串。

谁能帮忙?

【问题讨论】:

  • 你试过什么?您是否查看过任何字符串方法,尤其是 slice notation

标签: python loops for-loop


【解决方案1】:

这在 Python 中非常简单,因为它处理索引的方式:

def every_n(s, n):
    return s[::n]

冒号之间的第三项表示“每个 n”。

查看Explain Python's slice notation 以更深入地了解如何在 Python 中进行切片。

【讨论】:

  • def forstrings(string,n): for x in string[:n]: print str(x) forstrings("denmark",2)
猜你喜欢
  • 1970-01-01
  • 2016-01-24
  • 2021-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多