python的輸入與輸出 python輸入與輸出

2021-10-19 03:25:57 字數 3511 閱讀 3419

python格式化輸出的方法

要使用 格式化字串字面值 ,請在字串的開始引號或三引號之前加上乙個 f 或 f 。在此字串中,你可以在 字元之間寫可以引用的變數或字面值的 python 表示式。

>>> yes_votes = 42_572_654

>>> no_votes = 43_132_495

>>> percentage = yes_votes / (yes_votes + no_votes)

>>> ' yes votes '.format(yes_votes, percentage)

' 42572654 yes votes 49.67%'

字串的 str.format() 方法需要更多的手動操作。你仍將使用 來標記變數將被替換的位置,並且可以提供詳細的格式化指令,但你還需要提供要格式化的資訊。

>>> yes_votes = 42_572_654

>>> no_votes = 43_132_495

>>> percentage = yes_votes / (yes_votes + no_votes)

>>> ' yes votes '.format(yes_votes, percentage)

' 42572654 yes votes 49.67%'

最後,你可以使用字串切片和連線操作自己完成所有的字串處理,以建立你可以想象的任何布局。字串型別有一些方法可以執行將字串填充到給定列寬的有用操作。

也可以用 repr() or str() 函式將任何值轉化為字串輸出。

>>> s = 'hello, world.'

>>> str(s)

'hello, world.'

>>> repr(s)

"'hello, world.'"

>>> str(1/7)

'0.14285714285714285'

>>> x = 10 * 3.25

>>> y = 200 * 200

>>> s = 'the value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'

>>> print(s)

the value of x is 32.5, and y is 40000...

>>> # the repr() of a string adds string quotes and backslashes:

... hello = 'hello, world\n'

>>> hellos = repr(hello)

>>> print(hellos)

'hello, world\n'

>>> # the argument to repr() may be any python object:

... repr((x, y, ('spam', 'eggs')))

"(32.5, 40000, ('spam', 'eggs'))"

格式化字串長度

將pi捨入到小數點後三位:

>>> import math

在 ':' 後傳遞乙個整數可以讓該字段成為最小字元寬度。這在使列對齊時很有用。:

>>> table =

>>> for name, phone in table.items():

... print(f' ==> ')

sjoerd ==> 4127

jack ==> 4098

dcab ==> 7678

其他的修飾符可用於在格式化之前轉化值。 '!a' 應用 ascii() ,'!s' 應用 str(),還有 '!r' 應用 repr():

>>> animals = 'eels'

>>> print(f'my hovercraft is full of .')

my hovercraft is full of eels.

>>> print(f'my hovercraft is full of .')

my hovercraft is full of 'eels'.

format() 方法

str.format() 方法的基本用法如下所示:

>>> print('we are the {} who say "{}!"'.format('knights', 'ni'))

we are the knights who say "ni!"

花括號和其中的字元(稱為格式字段)將替換為傳遞給 str.format() 方法的物件。花括號中的數字可用來表示傳遞給 str.format() 方法的物件的位置。

>>> print(' and '.format('spam', 'eggs'))

spam and eggs

>>> print(' and '.format('spam', 'eggs'))

eggs and spam

如果在 str.format() 方法中使用關鍵字引數,則使用引數的名稱引用它們的值。:

>>> print('this is .'.format(

... food='spam', adjective='absolutely horrible'))

this spam is absolutely horrible.

位置和關鍵字引數可以任意組合:

>>> print('the story of , , and .'.format('bill', 'manfred',

other='georg'))

the story of bill, manfred, and georg.

>>> table =

>>> print('jack: ; sjoerd: ; '

... 'dcab: '.format(table))

jack: 4098; sjoerd: 4127; dcab: 8637678

這也可以通過使用 '**' 符號將 table 作為關鍵字引數傳遞。

>>> table =

>>> print('jack: ; sjoerd: ; dcab: '.format(**table))

jack: 4098; sjoerd: 4127; dcab: 8637678

這在與內建函式 vars() 結合使用時非常有用,它會返回包含所有區域性變數的字典。

例如,下面幾行**生成一組整齊的列,其中包含給定的整數和它的平方以及立方:

>>> for x in range(1, 11):

... print(' '.format(x, x*x, x*x*x))

1 1 1

2 4 8

3 9 27

4 16 64

5 25 125

6 36 216

7 49 343

8 64 512

9 81 729

10 100 1000

鍵盤輸入

python提供了 input() 內建函式從標準輸入讀入一行文字,預設的標準輸入是鍵盤。

str = input("請輸入:");

print ("你輸入的內容是: ", str)

python 的輸入與輸出

輸出 在此之前我們就接觸過print 這個函式了,但還有些用法比如 在同時接受多個字串用逗號隔開 就可以連成一串輸出 此時我們進入的是python互動式 print yangshen nan 1704 輸出 yangshen nan 1704 注 在print 函式裡面依次列印每個字串,遇到逗號就會...

Python的輸入與輸出

2.輸入內容不回顯 3.數值比較 4.格式化輸出 5.浮點型 6.整數 7.百分號的實現 8.輸入輸出練習 python3.x input 接收任意資料型別 python3.x中沒有raw input input num num 2 2 input num num abc abc import ge...

python輸入與輸出

python輸入與輸出 python是一門跨平台 開源 免費的解釋型高階動態程式語言,支援偽編譯將python源程式轉換為位元組碼來優化程式和提高執行速度,支援使用py2exe pyinstaller或cx freeze工具將python程式轉換為二進位制可執行檔案。輸出函式print print ...