python - 在 Python 中创建 n 个字符串

标签 python

我从用户那里得到一个数字 (n)。 只是

n = int(input())

之后,我必须创建 n 个字符串并从用户那里获取它们的值。

i = 0;

while (i < n):
    word = input() # so here is my problem: 
                   # i don't know how to create n different strings
    i += 1

如何创建n个字符串?

最佳答案

你需要使用一个列表,像这样:

n = int(input())
i = 0
words = []
while ( i < n ):
    word = input()
    words.append(word)
    i += 1

此外,最好将此循环创建为 for 循环:

n = int(input())
words = []
for i in range(n):
    words.append(input())

关于python - 在 Python 中创建 n 个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39994332/

相关文章:

python - 在python中使用lambda表达式在循环内生成函数

python - Putty:_tkinter.TclError:无显示名称且无显示环境变量

python - 在 map 上绘制不同大小的圆圈

python - 使用 Selenium 选择具有更改值的单选按钮

python - 如何从多个实体中删除 db.BlobProperty?

python - 属性错误 : 'module' object has no attribute 'choice'

python - Jupyter Notebook 只有 Python [conda root] 和 Python [default] 内核

python - a[ :]=b and a=b[:] 之间有什么区别

python - 无法注册 Google Assistant 设备型号

python - 如何修补 ConfigParser 键/值?