程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Common usage of standard library function print() in Python to output (print out) strings

編輯:Python

This blog post is used to record Python Standard library functions in print() Common usage of , as time goes on , There may be updates .

Catalog

  • 01- A word of print() Statement outputs several strings
  • 02- The first is to format the output ( and C Linguistic printf Function similar to )
  • 03- Second format output : use str.format()

01- A word of print() Statement outputs several strings

str1 = 'hello suwenhao'
print(" Output :", str1, 'Nice to meet you!')

The operation results are as follows :

02- The first is to format the output ( and C Linguistic printf Function similar to )

The sample code is as follows :

weight = 21
print("My name is %s and weight is %d kg!" % ('Zara', weight))

The operation results are as follows :

python The string formatting symbols are as follows :

The format operator auxiliary instructions are as follows :

About the usage of formatting operator auxiliary instructions , With the last “m.n” For example .
The sample code is as follows :

f1 = 12.456
print('#%f#' % f1)
print('#%10f#' % f1)
print('#%10.2f#' % f1)
print('#%0.2f#' % f1)
print('#%-10.2f#' % f1)

The operation results are as follows :

03- Second format output : use str.format()

The sample code is as follows

weight = 21
str1 = 'Zera'
print("My name is {} and weight is {} kg!".format(str1, weight))

The operation results are as follows :

The first 03 Kind of , use str.format() Look at the effect that seems to be achieved and 02 Same species , But it's actually more flexible 、 More powerful .
Its flexibility is reflected in :
① You can set the order of formatting parameters , As the following example :

>>> "{1} {0} {1}".format("hello", "world") # Set the specified location 
'world hello world' # This is the result of the operation 

② You can set the name , As the following example

# Set the name directly 
print(" Blog name :{name}, Address {url}".format(name=" Haohong image algorithm ", url="blog.csdn.net/wenhao_ir"))
# Set the parameters through the dictionary 
site = {
"name": " Haohong image algorithm ", "url": "blog.csdn.net/wenhao_ir"}
print(" Blog name :{name}, Address {url}".format(**site))
# Set the parameters by the list index 
my_list = [' Haohong image algorithm ', 'blog.csdn.net/wenhao_ir']
print(" Blog name :{0[0]}, Address {0[1]}".format(my_list)) # "0" Is a must 

The results are shown in the following figure :

③ You can pass in objects .
The sample code is as follows :

class AssignValue(object):
def __init__(self, value):
self.value = value
my_value = AssignValue(6)
print('value The value is : {0.value}'.format(my_value)) # "0" It's optional 

The operation results are as follows :

④str.format() Strong compatibility 、 Good fit
For example, we need to output the number of rows and columns of a matrix , We use it str.format() It's very convenient , The code is as follows :

import numpy as np
A1 = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]], dtype='int8')
print(' matrix A1 The number of rows and columns of are {}'.format(A1.shape))


If you use the second 02 In this way , Just put A1.shape The two members of the first take out , Then do the output processing .
Sum up , We can use the 03 The third way is the third way .
The first 03 The control of digital format by these methods is shown in the following table :

add , The first 03 Ways of planting , If you want to output braces ( Escape brace ), You can do this as follows :

print("{} The corresponding position is {
{0}}".format("suwenhao"))

The operation results are as follows :


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved