【问题标题】:is there a way to iterate through a string by incrementing the char through string slicing?有没有办法通过字符串切片增加字符来遍历字符串?
【发布时间】:2015-10-06 00:22:52
【问题描述】:

例如,我想将变量 b 加一,以便循环现在检查字符串中的第二个字符。在这种情况下,字符“o”。 b +1 是我想要实现的目标。

a = 'Goodbye'
b = 'Good'
c = ''

for letter in a:
    if letter == b[0]:
        c += letter
        #b [+1]
        print(c)
    else:
        print("no")

【问题讨论】:

    标签: string python-3.x increment slice


    【解决方案1】:

    您可以使用b[1] 访问第二个字符,如果您需要逐步递增到b,则使用b[i] 并根据需要递增i。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 2014-07-10
      • 2022-07-17
      • 1970-01-01
      • 1970-01-01
      • 2018-05-15
      相关资源
      最近更新 更多