python打印多个变量

Like other programming languages, In python also, we can define and print the multiple variables. Here, we see how can we print the single and multiple variables using the print() function?

像其他编程语言一样,在python中,我们也可以定义和打印多个变量 。 在这里,我们看到如何使用print()函数打印单个和多个变量?

In Python, single variable can be printed like this,

在Python中,可以这样打印单个变量,

print(variable)

Example:

例:

# Python program to print single variable
name = "Mike"
age = 21
country = "USA"
# printing variables one by one
print(name)
print(age)
print(country)
print() # prints a newline
# printing variables one by one
# with messages
print("Name:", name)
print("Age:", age)
print("Country:", country)

Output:

输出:

Mike
21
USA
Name: Mike
Age: 21
Country: USA

打印多个变量 (Printing multiple variables)

There are following methods to print multiple variables,

有以下方法可以打印多个变量,

  • Method 1: Passing multiple variables as arguments separating them by commas

    方法1 :传递多个变量作为以逗号分隔它们的参数

  • Method 2: Using format() method with curly braces ({})

    方法2 :使用带有大括号({})的format()方法

  • Method 3: Using format() method with numbers in curly braces ({0})

    方法3 :将format()方法与大括号({0})中的数字一起使用

  • Method 4: Using format() method with explicit name in curly braces ({v})

    方法4 :在大括号({v})中使用具有显式名称的format()方法

  • Method 5: Using string concatenation

    方法5 :使用字符串连接

Let's understand each method in the details.

让我们详细了解每种方法。

Method 1:

方法1:

To print multiple variables using the print() function, we need to provide the variable names as arguments separated by the commas.

要使用print()函数打印多个变量,我们需要提供变量名称作为用逗号分隔的参数。

Note: print() function prints space after the value of each variable, space is the default value of sep parameter – which is an optional parameter in print() function, by using this parameter, we can specify the separator value.

注意: print()函数在每个变量的值之后打印空格,space是sep参数的默认值–这是print()函数中的可选参数,通过使用此参数,我们可以指定分隔符值。

Syntax:

句法:

print(variable1, varaible2, variable3, ...)

Example:

例:

# Python program to print multiple variables
name = "Mike"
age = 21
country = "USA"
# printing variables one by one
print("Printing normally...")
print(name, age, country)
print() # prints a new line
# Printing with comma seprator
print("Printing with comma seprator...")
print(name, age, country, sep=',')
print() # prints a new line
# printing variables with messages
print("Printing with messages...")
print("Name:", name, "Age:", age, "Country:", country)

Output:

输出:

Printing normally...
Mike 21 USA
Printing with comma seprator...
Mike,21,USA
Printing with messages...
Name: Mike Age: 21 Country: USA

Method 2:

方法2:

By using the new-style string formatting (format() method), we can also print the multiple variables. Here, we have to specify the curly braces ({}) where we have to print the values and in the format() method, provide the multiple variables separated by the commas.

通过使用新的字符串格式设置( format()方法),我们还可以打印多个变量。 在这里,我们必须指定花括号( {}) ,在其中我们必须打印值,并在format()方法中提供多个用逗号分隔的变量。

Syntax:

句法:

print("{} {} {}".format(variable1, variable2, variable2)

Example:

例:

# Python program to print multiple variables
# using format() method
name = "Mike"
age = 21
country = "USA"
print("{} {} {}".format(name, age, country))
print("Name: {}, Age: {}, Country: {}".format(name, age, country))

Output:

输出:

Mike 21 USA
Name: Mike, Age: 21, Country: USA

Method 3:

方法3:

By using the new-style string formatting with numbers (format() method), we can also print the multiple variables. This is similar to method 2 but here we can use the numbers inside the curly braces ({0}), it will help for reordering the values.

通过使用带数字新型字符串格式设置( format()方法),我们还可以打印多个变量。 这类似于方法2,但是在这里我们可以使用花括号( {0} )中的数字,这将有助于重新排列值。

Note: Number 0 represents the first variable in format() method, 1 represents the second, and so on.

注意:数字0代表format()方法中的第一个变量,数字1代表第二个变量,依此类推。

Syntax:

句法:

print("{0} {1} {2}".format(variable1, variable2, variable2)

Example:

例:

# Python program to print multiple variables
# using format() method with numbers
name = "Mike"
age = 21
country = "USA"
print("{0} {1} {2}".format(name, age, country))
print("Name: {0}, Age: {1}, Country: {2}".format(name, age, country))
print("Country: {2}, Name: {0}, Age: {1}".format(name, age, country))
# printing all values 2-2 times
print("{0} {0} {1} {1} {2} {2}".format(name, age, country))

Output:

输出:

Mike 21 USA
Name: Mike, Age: 21, Country: USA
Country: USA, Name: Mike, Age: 21
Mike Mike 21 21 USA USA

Method 4:

方法4:

By using the new-style string formatting with explicit names (format() method), we can also print the multiple variables. This is similar to method 3 but here we can use the explicit names inside the curly braces ({n}), it will help for remembering the order and variable names.

通过使用带有显式名称新型字符串格式设置( format()方法),我们还可以打印多个变量。 这类似于方法3,但在这里我们可以在花括号( {n} )中使用显式名称,这将有助于记住顺序和变量名称。

Syntax:

句法:

print("{v1} {v2} {v3}".format(v1=variable1, v2=variable2, v3=variable2)

Example:

例:

# Python program to print multiple variables
# using format() method with explicit names
name = "Mike"
age = 21
country = "USA"
print("{n} {a} {c}".format(n=name, a=age, c=country))
print("Name: {n}, Age: {a}, Country: {c}".format(n=name, a=age, c=country))
print("Country: {c}, Name: {n}, Age: {a}".format(n=name, a=age, c=country))
# printing all values 2-2 times
print("{n} {n} {a} {a} {c} {c}".format(n=name, a=age, c=country))

Output:

输出:

Mike 21 USA
Name: Mike, Age: 21, Country: USA
Country: USA, Name: Mike, Age: 21
Mike Mike 21 21 USA USA

Method 5:

方法5:

We can print multiple variables by concatenating them as a string.

我们可以通过将多个变量串联为字符串来打印多个变量。

Syntax:

句法:

print(str(variable1) + str(variable2) + str(variable3))

Note:

注意:

  • If we want to display any message or separator, we can also concatenate them with the variables.

    如果要显示任何消息或分隔符,也可以将它们与变量连接起来。

  • If a variable is a string, then there is no need to use str().

    如果变量是字符串,则无需使用str()

Example:

例:

# Python program to print multiple variables
# using string concatenation
name = "Mike"
age = 21
country = "USA"
print("Without separator...")
print(name + str(age) + country)
print("Separating by commas...")
print(name + "," + str(age) + "," + country)
print("Printing with messages...")
print("Name: " + name + " Age: " + str(age) + " Country: " + country)

Output:

输出:

Without separator...
Mike21USA
Separating by commas...
Mike,21,USA
Printing with messages...
Name: Mike Age: 21 Country: USA

翻译自: https://www.includehelp.com/python/print-multiple-variables.aspx

python打印多个变量

python打印多个变量_在Python中打印多个变量相关推荐

  1. java打印图片到页面_在Java中打印BufferedImage的正确方法

    这是我的一个Java项目中的一个.此代码将在打印机页面上缩放和打印一个图像. 你这样称呼它: printButton.addActionListener(new ActionListener() { ...

  2. c语言指针访问 静态变量_使用C中的指针访问变量的值

    c语言指针访问 静态变量 As we know that a pointer is a special type of variable that is used to store the memor ...

  3. python打印自动换行如何解决_解决python DataFrame 打印结果不换行问题

    解决python DataFrame 打印结果不换行问题 如下所示: 加入代码: pd.set_option('display.width', 5000) 补充知识:Python 实现不换行打印字符的 ...

  4. 关于python变量_关于python变量练习题

    第一题(数字相加) age=20 new_age=age+1 print(new_age) 第二题(字符串相加) name='xiu' new_name=name+'hb' print(new_nam ...

  5. python设置环境变量_小白Python进行中

    一.安装 安装包的下载 在官网进行下载,我选用Python3.8.0. Welcome to Python.org​www.python.org 安装 安装的时候可以借鉴该视频. Windows 10 ...

  6. python程序运行结果不停_关于python:在进程运行时不断打印Subprocess输出

    要从我的python脚本启动程序,我使用以下方法: def execute(command): process = subprocess.Popen(command, shell=True, stdo ...

  7. 88是python语言的整数类型_少儿Python编程_第三讲:常量变量和数据类型

    无论使用哪一种编程语言,甚至是学习数学.物理,都需要掌握常量.变量.表达式的概念和用法.本讲将带领读者进入编程世界,学习程序的基本元素. 3.1 基本数据类型 基本数据类型有数值型.布尔型和字符型.它 ...

  8. python中的打印是什么意思_对python:print打印时加u的含义详解

    对python:print打印时加u的含义详解 u:表示unicode字符串 不是仅仅是针对中文, 可以针对任何的字符串,代表是对字符串进行unicode编码. 一般英文字符在使用各种编码下, 基本都 ...

  9. python逐步回归筛选变量_利用python实现逐步回归

    逐步回归的基本思想是将变量逐个引入模型,每引入一个解释变量后都要进行F检验,并对已经选入的解释变量逐个进行t检验,当原来引入的解释变量由于后面解释变量的引入变得不再显著时,则将其删除.以确保每次引入新 ...

最新文章

  1. 这 23 道题,全世界的数学家花费 100 年时间,只解答了一半
  2. GT Transceiver的复位与初始化(4)RX初始化和复位流程
  3. 吴恩达:AI是时候从大数据转向「小数据」了
  4. runtimeexception异常_应用系统的异常管理-持续更新
  5. 兩台SQL Server數據同步解決方案
  6. ionic + cordova 使用 cordova-gallery-api 获取本地相册所有图片
  7. C Shuffle Cards
  8. 【深入浅出etcd系列】1. 架构概览
  9. 福利网站!程序员面试——算法工程师面试大全第三部分
  10. azure云数据库_在Azure SQL数据库中实现动态数据屏蔽
  11. jq上传本地文件到服务器,jq实现前端文件上传
  12. Oracle 自定义函数、存储过程
  13. - 动规讲解基础讲解一——01背包(模板)
  14. Git生成并添加SSH key[并添加到Github]
  15. css属性table
  16. 树状图 - Dendrogram
  17. 游戏策划入门教程(前言)
  18. 区块链中的交易是什么意思
  19. Hypervisor介绍
  20. u-boot scsi sata源码解析

热门文章

  1. mysql通过集合查询_MySQL使用集合函数进行查询操作实例详解
  2. c3等待加载样式 vue_Vue.js__简易加载等待动画
  3. DataParallel 和 DistributedDataParallel 的区别和使用方法
  4. 使用ogg实现oracle到kafka的增量数据实时同步
  5. Kubernetes在上汽集团云平台及AI方面的应用
  6. mysql安装前的系统准备工作(转)
  7. atitit.atiOrmStoreService 框架的原理与设计 part1  概述与新特性
  8. 【Android】11.3 屏幕旋转和场景变换过程中GridView的呈现
  9. repadmin查看域控之间的复制状态
  10. HashMap get不出对象时出错 解决