Python:在循环中连接字符串和整数

标签 python string string-concatenation

我有一个整数列表,我想将它们连接成一个循环。

这是我目前的情况

a = [3, 4, 6]

temp = []
for i in a:
    query = 'Case:' + str(i)
    temp.append(query)

print(' OR '.join(temp))

>>> Case:3 OR Case:4 OR Case:6 

有没有更好的写法?

最佳答案

是的,你可以使用生成器表达式和 str.join

' OR '.join('Case: {}'.format(i) for i in a)

示例/演示 -

>>> a = [3, 4, 6]
>>> ' OR '.join('Case: {}'.format(i) for i in a)
'Case: 3 OR Case: 4 OR Case: 6'

关于Python:在循环中连接字符串和整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31868675/

相关文章:

python - 使用 pandas 写入 sql 数据库

python - 如何解决ModuleNotFoundError : No module named 'srsly.msgpack.util' in PyInstaller?

string - 查找到所有子串的编辑距离的算法

MySQL。函数中的逻辑错误应返回大写字母前带有空格的字符串

string - golang 将 "type []string"转换为字符串

python - Django 过滤器查询 - 不起作用

python - 使用 python 和 numpy 计算正则化损失函数的梯度

ruby 正则表达式 : split string with match beginning with either a newline or the start of the string?

arrays - 如何在VBA中将简单字符串转换为字节数组?

Java ArrayList<String> 到单个字符串而不使用循环