python - 连接字符串和变量值

标签 python r concatenation string-concatenation

我想在 Python 3 中连接字符串和变量值。 例如,在 R 中,我可以执行以下操作:

today <- as.character(Sys.Date())
paste0("In ", substr(today,1,4), " this can be an R way") 

R 中执行此代码会产生 [1]“In the year 2018 R is so straightforward”

Python 3.6 中尝试过类似的东西:

today = datetime.datetime.now()
"In year " + today.year + " I should learn more Python"

today.year 本身产生 2018,但整个连接产生错误:'int' object is not callable

在 Python3 中连接字符串和变量值的最佳方法是什么?

最佳答案

您可以尝试使用 str() 将 today.year 转换为字符串。

应该是这样的:

"In year " + str(today.year) + " I should learn more Python"

关于python - 连接字符串和变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48253554/

相关文章:

python - Airflow - 在 DockerOperator 中使用 run_id 作为卷名称

python - 许多大型 scipy 稀疏矩阵的内存高效存储

python - 如果两个 Tree 具有相同的值节点,则使用 set 进行计数

r - 插入符 : How to set up custom model deepnet

Haskell Concat 类型类

android - 如何串联多个小音频文件并创建结果文件?

VBA 连接字符串问题

python - int16、uint8 等图像背后的逻辑

r - 取消列出时防止元素重命名

在所有可能的组合上运行函数