python - 在同一行格式化字符串

标签 python string formatting

如果我有这样的程序:

   def P(x):
      # x is an integer
      print str(x) 

我想要一个输出,例如:

    >>> You chose the number: X

其中 X 是过程 P 中打印的结果。 如何在不更改程序的情况下做到这一点?

如果我这样做:

  print 'You chose the number: '
  P(x)

我会得到

 You chose the number: 
 X

如何让它们在同一行?

最佳答案

在第一个打印语句后添加一个trailing 逗号,以打印同一行中的下一个语句:-

print 'You chose the number: ',
P(x)

关于python - 在同一行格式化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13048337/

相关文章:

python - 同步时未记录的 Exchange ActiveSync 状态

python - 解析 JSON 文件时出现 KeyError

C函数删除字符串

c# - 在单个键盘快捷键中使用 R# 重新格式化代码

python - 如何导入 .pyc 编译的 python 文件并使用它

python - 将迭代函数应用于 pandas DataFrame 中的每个组

java - 尝试检测 if 语句中的字符

java - 如何在java中使用*分割字符串?

sql - 格式化具有任意小数位的 SQL 数字查询结果

python - 当逐行应用于 pandas DataFrame 中的 numpy.int 列时,整数格式规范 'd' 会产生 ValueError