【问题标题】:How to split input by length in Python如何在 Python 中按长度拆分输入
【发布时间】:2019-11-29 06:58:05
【问题描述】:

我正在 Python 中创建一个加密程序,用户将在其中输入

input_list=input.split(len(8))
for i in input_list:
    input+n=i. #string
    n +=1

它会转:

input1="This is " 
input2=" the text" 
input3=" which is" 
input4=" decrypted"
input5="!"
print (input1+input2+input3+....)

并且输出将显示与输入相同的文本

【问题讨论】:

标签: python python-3.x list variables type-conversion


【解决方案1】:

我能想到的最接近的方法是使用列表或字典,例如(使用列表):

input = "This is the text which is decrypted!"
output = []
length = 8
for i in range(len(input))[::length]:
    output.append(input[i:i+length])
print(output[0])
print(output[1])
#...

【讨论】:

  • 可以写成更简单的range(0, len(input), length)。然后甚至转换为列表理解:output = [input[i:i+length] for i in range(0, len(input), length)]但是,需要指出的是,input 不应用作变量,因为它是一个内置名称。
  • 是的,我为错误的变量命名道歉 =.=
猜你喜欢
  • 1970-01-01
  • 2016-04-02
  • 2018-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-30
  • 1970-01-01
相关资源
最近更新 更多