【问题标题】:Is there a way for a for loop to change the type from list to str?有没有办法让 for 循环将类型从 list 更改为 str?
【发布时间】:2021-11-25 02:05:55
【问题描述】:

我怎样才能有一个字符串类型而不是列表的解决方案?我希望我的输出是 12345...49 而不是 [1, 2, ... ,49]

solution1=[]

for numbers in range(1,50):
  solution1.append(numbers)

print(solution1)

我找到的解决方案:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]

谁能帮我解决这个任务,因为我不知道如何实现。

【问题讨论】:

标签: python string for-loop


【解决方案1】:

将数字附加到字符串

solution1 = ''

for numbers in range(1,50):
  solution1 += str(numbers)

print(solution1) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多