字串初始化

2021-07-25 20:25:13 字數 4115 閱讀 9368

字串初始化使用字串初始化操作符即百分號—% 。

標記轉化說明符的開始。

在%左邊為格式化字串,右邊為被格式化的值。

>>> format='hello,%s.%s enough?'

>>> values=('world','cold')

>>> print

format % values

hello,world.cold enough?

>>>

格式化字串的%s部分為轉換說明符,他們標記了需要插入轉換值的位置。s表示值會被格式化為字串,如果要格式化實數,用f說明轉化說明符的型別,同時需要提供精度:乙個句點再加上保留的小數字數。

>>> format='pi with three decimals: %.3f'

>>>

from math import pi

>>>

print format % pi

pi with three decimals: 3.142

>>>

序列會被解釋為乙個值,元組和字典可以格式化為字串。

如果右運算元為元組,每一位元素都會被單獨格式化,每乙個值都需要乙個對應的轉化說明符。

>>> 

'%s plus %s equals %s' % (1,1,2)

1 plus 1 equals 2

>>>

—表示左對齊;+表示在轉換值之前要加上正負號 ; 「 」表示正數之前保留空格;0表示轉換值若位數不夠則用0填充。

>>> 

from math import pi

>>>

'%010.2f' % pi

'0000003.14'

>>>

'%-10.2f' % pi

'3.14 '

>>>

print ('%5d' % 10 +'\n'+'%5d' % -10)

10-10

>>>

print ('%+5d' % 10 +'\n'+'%+5d' % -10)

+10-10

>>>

*轉換後的字串應具有指定的寬度。如果是 ,則寬度會從值元組中讀出。

*如果轉換的是實數,精度值就表示出現在小數點後的位數。如果轉換的是字串,那麼該數字就是最大字段寬度。如果是 ,精度從元組中讀出來。

>>> 

from math import pi

>>>

'%10.2f' % pi

' 3.14'

>>>

'%*.2f' % (10,pi)

' 3.14'

>>>

'%*.*f' % (10,2,pi)

' 3.14'

>>>

'%.*f' % (2,pi)

'3.14'

>>>

'%5.2s' % 'abcdef'

' ab'

>>>

'%*.2s' % (5,'abcdef')

' ab'

>>>

'%*.*s' % (5,2

'abcdef')

' ab'

>>>

'%.*s' % (2,'abcdef')

'ab'

>>>

d , i —–帶符號的十進位制正數

u —–不帶符號的十進位制

o —–不帶符號的八進位制

x , x —–不帶符號的十六進製制

e , e —–科學計數法表示的浮點數

f , f —–十進位制浮點數

g , g —–指數大於-4或者小於精度值則和e相同,否則與f相同

c —–單字元(接受整數)

r —–字串(使用repr轉換的物件)

s —–字串(使用str轉換的物件)

number

is %d' % 17

'the

number

is17'

>>> 'the

number

is %i' % 17.567

'the

number

is17'

>>> 'the

number

is %o' % 17

'the

number

is21'

>>> 'the

number

is %x' % 17

'the

number

is11'

>>> 'the

number

is %f' % 17.567

'the

number

is17.567000'

>>> 'the

number

is %e' % 17.567

'the

number

is1.756700e+01'

>>> 'the

number

is %g' % 17.567

'the

number

is17.567'

>>> 'the

number

is %g' % 0.0000017567

'the

number

is1.7567e-06'

>>> 'the letter is %c' % 'a'

'the letter is a'

>>> 'the letter is %r' % 42l

'the letter is

42l'

>>> 'the letter is %s' % 42l

'the letter is

42'

字串格式化示例

#使用給定的寬度列印格式化後的**列表

width=input('please enter width:')

price_width=10

item_width=width-price_width

header_format='%-*s%*s'

format ='%-*s%*.2f'

print

'='*width

print header_format %(item_width,'item',price_width,'price')

print

'-'*width

print

format

print

format

%(item_width,'pears',price_width,0.5)

print

format

%(item_width,'cantal',price_width,1.92)

print

format

%(item_width,'dried(16 oz)',price_width,8)

print

format

%(item_width,'prunes(4 1bs)',price_width,12)

結果如下

>>>********************=== restart ********************===

>>>

please enter width:35

***********************************

item price

-----------------------------------

pears 0.50

cantal 1.92

dried(16 oz) 8.00

prunes(4 1bs) 12.00

>>>

字串初始化

在c語言程式設計中,當我們宣告乙個字串陣列的時候,常常需要把它初始化為空串。總結起來有以下三種方式 1 char str 10 2 char str 10 3 char str 10 str 0 0 第 1 2 種方式是將str陣列的所有元素都初始化為 0 而第 3 種方式是只將str陣列的第乙個元...

字串初始化

在 裡,經常會用字串進行初始化操作。比如 void func void 對於上面2條初始化語句,它們的區別是什麼呢?1,首先,hello world 是乙個字串常量,存放在靜態常量區。2,str1是乙個字元陣列,分配在棧上,儲存空間由 hello world 的長度 含 0 決定,存放的內容由 he...

C 列表初始化,字串初始化

列表初始化 int a 0 int a 全面應用 int a int a 0 預設初始化 定義變數時,沒有指定初值,則變數被預設初始化。定義函式體外的變數被初始化為0,函式體內部的內建型別變數不被初始化。字串初始化 string s1 string s2 s1 string s2 s1 string...