Python 字串的格式化和使用者輸入

2021-09-25 21:35:44 字數 3549 閱讀 9056

'''''

知識點彙總;

1-字串格式化輸出方法一: %

1-print('名字是 %s,年齡是%s' % (name ,age))

2- %s ---字串-----相當於執行了str()

3- (name ,age) 只能是元組,不能是列表

4- 多個資料的列印,一定是元組

5- %d--十進位制

6- %f--6位小數

7- %x--

8-指定長度列印----數值和字串一樣的

1- %5d 右對齊 ,不足左邊補空格

2- %-5d 左對齊 ,不足右邊補空格

3- 補0 %05d

9- 十六進製制:%#x # 加乙個 0x

10 小數--float

1- 預設是6位

2- 指定保留小數字數- %.3f-----進行了四捨五入

3- %6.3f ---- 6代表總長度(包括 . )

4- %08.3f ---- 補0

2-字串格式化輸出方法二: format()---固定的 {}

1- 順序填坑:

1- 可以有元素多,不能有元素少!

print('名字是 {},年齡是 {}'.format(name ,age))

2- 下標填坑:

1- 不能下標越界 indexerror: tuple index out of range

print('名字是 ,年齡是 '.format(name ,age))

3- 變數方法

1- print('名字是 ,年齡是 '.format(name='tom' ,age = 18))

4-指定長度輸出:

1- 1- 數值型:右對齊,左補齊

2- 字串:左對齊,右補齊

2- > 右對齊

3- < 左對齊

4- ^ 中間對齊 ---異或

5- 數值補0 ,一般是右對齊 , 左補0 ,不改變值

6- 字串本身帶花括號 }

3- python 3.6 以後 f''

print(f'名字是,年齡是')

4- 轉義符 \

print('name is \n tom')

5- input()---控制台的終端輸入

1- 有返回值---str

2- 如果對得到的值進行算術---int()、float()

3- 使用者的輸入是以乙個回車符結束---不敲回車就死等

'''''

'''format方式:

1- 順序填值

---對於值是字串---左對齊;數值型別---右對齊

> 右對齊

< 左對齊

^ 中間對齊 print('我叫:,年齡是:'.format(name,age))

2- 下標填值

print('我叫:,年齡是:'.format(name,age))

3- 變數填值

print('我叫:,年齡是:'.format(name='jack',age=16))

'''# ------------1.%--------------------------------------------

name =

'tom'

height =

170# %s - 相當於 - str()

print

('我叫%s,身高%scm'

%(name,height)

)# 兩個元素以上用元祖

print

('我叫%s,身高%dcm'

%(name,height)

)# %d----十進位制

# 指定長度

print

('%d'%68

)# 1.正數-右對齊,左補齊(空格)

print

('%5d'%68

)print

('%05d'%68

)# 2.負數-左對齊,右補齊(空格) 一般數字不會去右補0

print

('%-5d'%68

)# 3.#格式化的%f

print

('%f'

%3.1415926

)#--預設是6位 --會四捨五入

print

('%.3f'

%3.1415926

)# 小數點後保留3位

print

('%6.3f'

%3.1415926

)# 前面6--整體長度--不足 前面補空格

# ------------2.format--------------------------------------------

# 1.順序填值

name =

'sha'

age =

20print

('名字是{},年齡是{}'

.format

(name ,age)

)# 對齊: > 右對齊 < 左對齊 ^ 中間對齊

print

('名字是,年齡是'

.format

(name ,age)

)print

('名字是,年齡是'

.format

(name ,age)

)print

('名字是,年齡是'

.format

(name ,age)

)# 不夠補*

print

('名字是,年齡是'

.format

(name ,age)

)# 2.下標填值

name =

'sha'

age =

20# 下標填值可以

print

('名字是 ,年齡是 '

.format

(name ,age)

)print

('名字是 ,年齡是 '

.format

(name ,age)

)print

('名字是 ,年齡是 '

.format

(name ,age)

)#3. 變數填值

print

('名字是 ,年齡是 '

.format

(name=

'sha'

,age=20)

)#------------------切記:三種不能混用------------------------------

# 字串本身帶有{}

name =

'sha'

age =

20print

('我叫{},年齡是{},我本來就有乙個}'

.format

(name,age)

)# 擴充套件內容:

name =

'sha'

print

(f'我叫'

)name =

'sha'

data =

""print

(f"}}"

)

Python 字串格式化 format 的用法

python2.6 開始,新增了一種格式化字串的函式 str.format 它增強了字串格式化的功能。基本語法是通過 和 來代替以前的 其優點如下 1.不需要理會資料型別的問題,在 方法中 s只能替代字串型別 2.單個引數可以多次輸出,引數順序可以不相同 3.填充方式十分靈活,對齊方式十分強大 fo...

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中的一致。右邊的 值組 如果有兩個及以上的值則需要用小括號括起來,中間用短號隔開。重點來看左邊的部分。左邊部分的最簡單...