python - 将字符串与数组值连接起来

标签 python arrays

我正在使用 Python 并希望能够创建一个数组,然后将值与特定格式的字符串连接起来。我希望下面能解释我的意思。

name_strings = ['Team 1', 'Team 2']
print "% posted a challenge to %s", %(name_strings)

name_strings 中的每个值都将放置在 %s 位置。非常感谢任何帮助。

最佳答案

一种方法可能是将数组扩展到 str format函数...

array_of_strings = ['Team1', 'Team2']
message = '{0} posted a challenge to {1}'
print(message.format(*array_of_strings))
#> Team1 posted a challenge to Team2

关于python - 将字符串与数组值连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20778304/

相关文章:

python - python OpenCV(cv2)putText方法中的中文编码

c - C中的动态内存分配?

java - 对字符串和整数执行 + 的混淆

java - 如何将值存储在二维数组中以供以后使用?

JavaScript - 在不影响索引的情况下将元素插入数组?

Python:循环遍历 if 语句的 elif 部分

python - 如何在 scikit 中对分类数据使用一种热编码器?

python - 在模拟上设置属性不起作用

python - 根据不同列中的值替换特定列中的 pandas 数据帧值

javascript - 如何将 PHP 数组转为 Javascript