Python 格式化字串與format格式化

2021-09-25 14:04:20 字數 2111 閱讀 4110

%號格式化

佔位符格式

描述%d

有符號的整數

%s字串

%c字元以及asicc碼

%o無符號八進位制整數

%x/%x

無符號十六進製制整數

%x無符號十六進製制整數

%e/%e

科學記數法

%f浮點數

name =

't'age =

18print

("his name is %s ,age is %d."

%(name,age)

)print

("his name is"

,name,

",age is"

,age,

".")

print

("his name is "

+name+

" ,age is "

+str

(age)

+"."

)

兩個百分號%%,第乙個%相當於轉義字元

a =

100"%d%%",%a

>>

>

100%

print

('name:{},age:{}'

.format

('tom',18

))>>

>name:tom,age:

18

print

('name:{},age:{},'

.format

('tom',18

,address =

"gongshu"

))

關鍵字放在位置引數的後面

」\「換行符

print

("第乙個元素是:,第二個元素是:,第三個元素是;第四個元素是:,第五個元素是:."

flush: whether to forcibly flush the stream.

語法格式:

print

(value,..

., sep=

' ', end=

'\n'

,file

=sys.stdout, flush=

false

)

等價於

print

(value1,value2,..

.,valuen,sep=

'',end=

'\n'

,file

=sys.stdout,flush=

false

)

a* 表示乙個元祖

Python 字串格式化

字串格式化 s 格式化為字串 format hello,s.s enough for ya?values world hot print format values hello,world.hot enough for ya?f 格式化為實數 浮點數 format pi with three dec...

python字串格式化

字串的格式化 在python中也有類似於c中的printf 的格式輸出標記。在python中格式化輸出字串使用的是 運算子,通用的形式為 其中,左邊部分的 格式標記字串 可以完全和c中的一致。右邊的 值組 如果有兩個及以上的值則需要用小括號括起來,中間用短號隔開。重點來看左邊的部分。左邊部分的最簡單...

Python字串格式化

字串的格式化 在python中也有類似於c中的printf 的格式輸出標記。在python中格式化輸出字串使用的是 運算子,通用的形式為 其中,左邊部分的 格式標記字串 可以完全和c中的一致。右邊的 值組 如果有兩個及以上的值則需要用小括號括起來,中間用短號隔開。重點來看左邊的部分。左邊部分的最簡單...