日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python打印多个变量_在Python中打印多个变量

發布時間:2025/3/11 python 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python打印多个变量_在Python中打印多个变量 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 USAName: 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 USAPrinting with comma seprator... Mike,21,USAPrinting 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() methodname = "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 numbersname = "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 namesname = "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 concatenationname = "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中打印多个变量的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。