打印变量后的 Python 打印文本 [英] Python printing text after printing a variables

查看:54
本文介绍了打印变量后的 Python 打印文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想在像这样打印变量后打印一些文本:

So I wanna print some text after i print my variables like this:

print('Blablabla' var ' blablabla')

现在看起来像这样:

 print('The enemey gets hit for %d' % damage)

我想在打印损坏变量后打印Hitpoints"这个词.

I wanna print the word "Hitpoints" after I've printed the damage variable.

推荐答案

只包含命中点:

print('The enemey gets hit for %d hitpoints' % damage)

格式化操作符%很强大,看看所有占位符选项.然而,它打算被淘汰以支持 str.format:

The formatting operator % is very powerful, have a look at all the placeholder options. It is, however, intended to be phased out in favor of str.format:

print('The enemey gets hit for {} hitpoints'.format(damage))

或者,您可以将 damage 的值转换为字符串,并用 + 连接字符串:

Alternatively, you can convert the value of damage to a string, and concatenate strings with +:

print('The enemy gets hit for ' + str(damage) + ' hitpoints')

这篇关于打印变量后的 Python 打印文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆