在 Python 中如何打印字符串和变量
Python 是一种多功能的、灵活的语言——通常有不止一种方法来实现某件事。
在本教程中,你将看到一些可以将字符串和变量一起打印的方法。
让我们开始吧!
如何使用 Python 中的 print()
函数
要在 Python 中打印任何东西,你可以使用 print()
函数——也就是在 print
关键字后面加上个小括号,()
。
#如何打印一个字符串
print("Hello world")
#如何打印一个整数
print(7)
#如何打印一个变量
#to just print the variable on its own include only the name of it
fave_language = "Python"
print(fave_language)
#输出
#Hello world
#7
#Python
如果省略括号,则会出现错误:
print "hello world"
#运行代码的输出:
#File "/Users/dionysialemonaki/python_articles/demo.py", line 1
# print "hello world"
# ^^^^^^^^^^^^^^^^^^^
#SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
如果你在 Visual Studio Code 中使用 Python 扩展编写 Python 代码,你还会得到一个下划线和一个提示,这些都表明有些事情不太对劲:
测试
如前所述,print
语句用于输出各种信息,包括文本和数字、变量和其他数据类型。
你还可以在一条语句中打印与变量组合的文本(或字符串)。
在接下来的部分中,你将看到一些不同的方法来执行此操作。
如何使用连接在 Python 中打印变量和字符串
根据字典,连接意味着以链或系列的形式将(事物)链接在一起。
你可以通过使用 Python 加法运算符 +
将各种事物彼此相加来实现这一点。
请记住,连接仅用于字符串,因此如果要与其余字符串连接的变量是整数数据类型,则必须使用 str()
函数将其转换为字符串。
在下面的示例中,我想打印变量的值以及其他一些文本。
我在双引号中添加字符串,变量名外部没有任何符号包裹它,使用加法运算符将它们链接在一起:
fave_language = "Python"
print("I like coding in " + fave_language + " the most")
#output
#I like coding in Python the most
在字符串连接中,你必须自己添加空格,因此如果在前面的示例中我没有在引号内包含任何空格,则输出将如下所示:
fave_language = "Python"
print("I like coding in" + fave_language + "the most")
#output
#I like coding inPythonthe most
你甚至可以单独添加空格:
fave_language = "Python"
print("I like coding in" + " " + fave_language + " " + "the most")
#输出
#I like coding in Python the most
这不是打印字符串和变量的首选方式,因为它容易出错且耗时。
如何在 Python 中打印逗号分隔的变量和字符串
你可以在一个打印语句中一齐打印文本和变量,用逗号分隔。
first_name = "John"
print("Hello",first_name)
#输出
#Hello John
在上面的例子中,我首先在双引号中包含了一些我想打印的文本——在这个例子中,文本是字符串 Hello
。
在引号之后,我添加了一个逗号,它将这段文本与我随后包含的变量名称(在本例中为 first_name
)中保存的值分开。
我可以在变量后面添加更多文本,如下所示:
first_name = "John"
print("Hello",first_name,"good to see you")
#输出
#Hello John good to see you
这种方法也适用于多个变量:
first_name = "John"
last_name = "Doe"
print("Hello",first_name,last_name,"good to see you")
#输出
Hello John Doe good to see you
确保用逗号分隔所有内容。
所以,你要用逗号把文本和变量分开,也要把变量和其他变量分开,如上图所示。
如果在 first_name
和 last_name
之间没有加逗号,代码就会出现错误:
first_name = "John"
last_name = "Doe"
print("Hello",first_name last_name,"good to see you")
#输出
#File "/Users/dionysialemonaki/python_articles/demo.py", line 4
# print("Hello",first_name last_name,"good to see you")
# ^^^^^^^^^^^^^^^^^^^^
#SyntaxError: invalid syntax. Perhaps you forgot a comma?
正如你所看到的,Python 的错误信息是非常有用的,使调试过程更容易一些:)
如何在 Python 中使用字符串格式化打印一个变量和一个字符串
使用字符串格式化,在你想添加变量值的地方包括一个括号,{}
。
first_name = "John"
print("Hello {}, hope you're well!")
在这个例子中,有一个变量,first_name
。
在打印语句中,有一个双引号,里面有需要打印的文本。
在这里面,我在我想添加变量 first_name
的位置上添加了一个大括号。
如果我尝试运行这段代码,会有以下输出:
#输出
#Hello {}, hope you're well!
它实际上并没有打印 first_name
的值!
要打印它,我需要在字符串的末尾添加 .format()
字符串方法——也就是紧跟在闭合引号之后:
first_name = "John"
print("Hello {}, hope you're well!".format(first_name))
#输出
#Hello John, hope you're well!
如果有多个变量,则使用与要打印的变量数量一样多的花括号:
first_name = "John"
last_name = "Doe"
print("Hello {} {}, hope you're well!")
在此示例中,我创建了两个变量,并且我想一个接一个地打印这两个变量,因此我在要替换变量的位置添加了两组花括号。
现在,当涉及到 .format()
方法时,将变量名放入其中的顺序很重要。
因此,在方法中首先添加的变量名的值将在第一个花括号的位置,第二个将添加的变量名的值将在第二个花括号的位置,以此类推。
确保在方法中用逗号分隔变量名:
first_name = "John"
last_name = "Doe"
print("Hello {} {}, hope you're well!".format(first_name,last_name))
#输出
#Hello John Doe, hope you're well!
如果我把方法里面的名字顺序颠倒过来,输出结果会有所不同:
first_name = "John"
last_name = "Doe"
print("Hello {} {}, hope you're well!".format(last_name,first_name))
#输出
#Hello Doe John, hope you're well!
如何使用 f-strings
在 Python 中打印变量和字符串
与我们在上一节中看到的方法相比,f-strings
是一种更好、更易读、更简洁的实现字符串格式化的方法。
语法更简单,需要更少的手动工作。
创建 f-string
字符串的一般语法如下所示:
print(f"I want this text printed to the console!")
#输出
#I want this text printed to the console!
首先,在 print()
函数中,你在引号前加入字符 f
。
要在一行中打印一个带有字符串的变量,你又在同样的地方包括字符 f
——就在引号之前。
然后在引号内添加你想要的文本,在你想要添加变量值的地方添加一组大括号,里面是变量的名字。
first_name = "John"
print(f"Hello, {first_name}!")
#输出
#Hello, John!
如果要打印多个变量,你可以在第二个变量名称上再加一组大括号:
first_name = "John"
last_name = "Doe"
print(f"Hello, {first_name} {last_name}!")
#输出
#Hello, John Doe!
放置变量名称的顺序很重要,因此请确保根据所需的输出添加它们。
如果我颠倒了名称的顺序,我会得到以下输出:
first_name = "John"
last_name = "Doe"
print(f"Hello, {last_name} {first_name}!")
#输出
#Hello, Doe John!
总结
感谢你阅读本文!你现在知道了在 Python 中在一行中打印字符串和变量的几种不同方法。