Python 是一种多功能且灵活的语言——通常有多种方法可以实现某件事。
在本教程中,您将看到一些同时打印字符串和变量的方法。
让我们开始吧!
如何 print()
在 Python 中使用该函数
要在 Python 中打印任何内容,请使用 print()
函数 - 即 print
关键字后跟一组左括号和右括号, ()
.
#how to print a string
print("Hello world")
#how to print an integer
print(7)
#how to print a variable
#to just print the variable on its own include only the name of it
fave_language = "Python"
print(fave_language)
#output
#Hello world
#7
#Python
如果省略括号,则会出现错误:
print "hello world"
#output after running the code:
#File "/Users/dionysialemonaki/python_articles/demo.py", line 1
# print "hello world"
# ^^^^^^^^^^^^^^^^^^^
#SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
如果您使用带有 Python 扩展 ,您还会得到一条下划线和一条提示,它们都表明某些地方不太对劲:
如上所述,打印语句用于输出各种信息。这包括文本和数字数据、变量和其他数据类型。
您还可以在一个语句中打印与变量组合的文本(或字符串)。
您将在后面的部分中看到一些不同的方法来实现这一点。
如何使用连接在 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")
#output
#I like coding in Python the most
这不是打印字符串和变量的最佳方式,因为它容易出错并且耗时。
如何在 Python 中打印变量和字符串并用逗号分隔
您可以在一个打印语句中打印文本以及变量(以逗号分隔)。
first_name = "John"
print("Hello",first_name)
#output
#Hello John
在上面的例子中,我首先在双引号中包含了一些想要打印的文本——在本例中,文本是字符串 Hello
.
在结束的引号之后,我添加了一个逗号,将该部分文本与 first_name
我随后包含的变量名(在本例中)中保存的值分开。
我可以在变量后面添加更多文本,如下所示:
first_name = "John"
print("Hello",first_name,"good to see you")
#output
#Hello John good to see you
此方法也适用于多个变量:
first_name = "John"
last_name = "Doe"
print("Hello",first_name,last_name,"good to see you")
#output
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")
#output
#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
.
如果我尝试运行此代码,它将有以下输出:
#output
#Hello {}, hope you're well!
它实际上并没有打印出的值 first_name
!
在字符串末尾 .format()
添加
first_name = "John"
print("Hello {}, hope you're well!".format(first_name))
#output
#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))
#output
#Hello John Doe, hope you're well!
如果我颠倒方法中名称的顺序,输出将会有所不同:
first_name = "John"
last_name = "Doe"
print("Hello {} {}, hope you're well!".format(last_name,first_name))
#output
#Hello Doe John, hope you're well!
如何使用 Python 打印变量和字符串 f-strings
f-strings
与我们在上一节中看到的方法相比,是一种更好、更易读、更简洁的实现字符串格式化的方法。
语法更简单,需要的手动工作更少。
创建的一般语法 f-string
如下:
print(f"I want this text printed to the console!")
#output
#I want this text printed to the console!
内部的开引号和闭引号之前 f
包含字符 print()
。
要在一行中打印带有字符串的变量,您需要再次将字符放在 f
相同的位置 - 就在引号之前。
然后在引号内添加所需的文本,并在想要添加变量值的地方添加一组带有变量名的花括号:
first_name = "John"
print(f"Hello, {first_name}!")
#output
#Hello, John!
要打印多个变量,请添加另一组带有第二个变量名的花括号:
first_name = "John"
last_name = "Doe"
print(f"Hello, {first_name} {last_name}!")
#output
#Hello, John Doe!
变量名称的放置顺序很重要,因此请确保根据您想要的输出添加它们。
如果我颠倒了名字的顺序,我会得到以下输出:
first_name = "John"
last_name = "Doe"
print(f"Hello, {last_name} {first_name}!")
#output
#Hello, Doe John!
结论
感谢您阅读并坚持到最后!现在您知道了在 Python 中在一行中打印字符串和变量的几种不同方法。
它适合初学者,因为它从基础知识开始,逐渐发展到更高级的概念。您还可以构建五个项目并将您获得的所有新知识付诸实践。
祝你编码愉快!
发表评论 取消回复