Python之print函數詳解

來源:互聯網
上載者:User

標籤:copyto   舉例   tag   樣式   精度   紅色   clip   pos   底線   

輸出的 print 函數總結:

1. 字串和數實值型別
可以直接輸出

 

[python] view plain copy 
  1. >>> print(1)  
  2. 1  
  3. >>> print("Hello World")  
  4. Hello World  


2.變數
無論什麼類型,數值,布爾,列表,字典...都可以直接輸出

 

 

[python] view plain copy 
  1. >>> x = 12  
  2. >>> print(x)  
  3. 12  
  4. >>> s = ‘Hello‘  
  5. >>> print(s)  
  6. Hello  
  7. >>> L = [1,2,‘a‘]  
  8. >>> print(L)  
  9. [1, 2, ‘a‘]  
  10. >>> t = (1,2,‘a‘)  
  11. >>> print(t)  
  12. (1, 2, ‘a‘)  
  13. >>> d = {‘a‘:1, ‘b‘:2}  
  14. >>> print(d)  
  15. {‘a‘: 1, ‘b‘: 2}  


3.格式化輸出
類似於C中的 printf

 

 

[python] view plain copy 
  1. >>> s  
  2. ‘Hello‘  
  3. >>> x = len(s)  
  4. >>> print("The length of %s is %d" % (s,x))  
  5. The length of Hello is 5  


看看《Python基礎編程》中對格式化輸出的總結:

 

(1). %字元:標記轉換說明符的開始


(2). 轉換標誌:-表示靠左對齊;+表示在轉換值之前要加上加號或減號;“”(空白字元)表示正數之前保留空格;0表示轉換值若位元不夠則用0填充


(3). 最小欄位寬度:轉換後的字串至少應該具有該值指定的寬度。如果是*,則寬度會從值元組中讀出。


(4). 點(.)後跟精度值:如果轉換的是實數,精度值就表示出現在小數點後的位元。如果轉換的是字串,那麼該數字就表示最大欄位寬度。如果是*,那麼精度將從元組中讀出

 

(5).字串格式化轉換類型


轉換類型          含義

d,i                 帶符號的十進位整數
o                   不帶符號的八進位
u                   不帶符號的十進位
x                    不帶符號的十六進位(小寫)
X                   不帶符號的十六進位(大寫)
e                   科學計數法表示的浮點數(小寫)
E                   科學計數法表示的浮點數(大寫)
f,F                 十進位浮點數
g                   如果指數大於-4或者小於精度值則和e相同,其他情況和f相同
G                  如果指數大於-4或者小於精度值則和E相同,其他情況和F相同
C                  單字元(接受整數或者單字元字串)
r                    字串(使用repr轉換任意python對象)
s                   字串(使用str轉換任意python對象)

 

[python] view plain copy 
  1. >>> pi = 3.141592653  
  2. >>> print(‘%10.3f‘ % pi) #欄位寬10,精度3  
  3.      3.142  
  4. >>> print("pi = %.*f" % (3,pi)) #用*從後面的元組中讀取欄位寬度或精度  
  5. pi = 3.142  
  6. >>> print(‘%010.3f‘ % pi) #用0填充空白  
  7. 000003.142  
  8. >>> print(‘%-10.3f‘ % pi) #靠左對齊  
  9. 3.142       
  10. >>> print(‘%+f‘ % pi) #顯示加號或減號  
  11. +3.141593  


4.如何讓 print 不換行
在Python中總是預設換行的

 

 

[python] view plain copy 
  1. >>> for x in range(0,10):  
  2.     print(x)  
  3.   
  4.       
  5. 0  
  6. 1  
  7. 2  
  8. 3  
  9. 4  
  10. 5  
  11. 6  
  12. 7  
  13. 8  
  14. 9  


如果想要不換行,之前的 2.x 版本可以這樣 print x, 在末尾加上 ,
但在 3.x 中這樣不起任何作用
要想換行你應該寫成 print(x,end = ‘‘ )

 

 

[python] view plain copy 
  1. >>> for x in range(0,10):  
  2.     print (x,end = ‘‘)  
  3.   
  4.       
  5. 0123456789  



 

 

 

拼接字串:

 

[python] view plain copy 
  1. >>> "Hello""World"  
  2. ‘HelloWorld‘  
  3. >>> x = "Hello"  
  4. >>> y = "world"  
  5. >>> xy  
  6. Traceback (most recent call last):  
  7.   File "<pyshell#10>", line 1, in <module>  
  8.     xy  
  9. NameError: name ‘xy‘ is not defined  
  10. >>> x+y  
  11. ‘Helloworld‘  

 

 

pow函數:

[python] view plain copy 
  1. # 2**3%5(2的3次冪對5模數)  
  2. >>> pow(2,3,5)  
  3. 3  


然後很重要一點是類型可以自由地轉換,你賦什麼值,變數就是什麼類型,python會自動幫你管理

這點真讓我的C++思維轉不過來呢

 

[cpp] view plain copy 
    1. >>> x = 2  
    2. >>> type(x)  
    3. <class ‘int‘>  
    4. >>> x = 2.3  
    5. >>> type(x)  
    6. <class ‘float‘>  
    7. >>> x = [2,3]  
    8. >>> type(x)  
    9. <class ‘list‘>

 

python 輸出帶顏色原創 2016年09月29日 15:04:51
  • 標籤:
  • python-輸出 /
  • 顏色 /
  • Python /
  • 帶顏色
  • 1881

\033[顯示方式; 前景色彩; 背景色m

  1. 顯示方式 
    0(預設值) 
    1(高亮) 
    4(底線) 
    5(閃爍) 
    7(反顯) 
    22(非粗體) 
    24(非底線) 
    27(非反顯)

  2. 前景色彩 
    30(黑色) 
    31(紅色) 
    32(綠色) 
    33(黃色) 
    34(藍色) 
    35(洋紅) 
    36(青色) 
    37(白色)

  3. 背景色 
    40(黑色) 
    41(紅色) 
    42(綠色) 
    43(黃色) 
    44(藍色) 
    45(洋紅) 
    46(青色) 
    47(白色)

\033[1;31;40m 紅色 
\033[1;32;40m 綠色

  1. 使用方法 
    print ‘\033[5;31;2m%s\033[0m’ % num 

    print ‘\033[5;34;2m%s\033[0m’ % num 
     
    print ‘\033[5;35;2m%s\033[0m’ % num 
     
    … 
    … 
    … 
    有很多種樣式我就不一一舉例。

Python之print函數詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.