# 用戶互動 input、格式化輸出
# 1.接受用戶的輸入
# 在python3:input會將用戶輸入的所有內容都存成字串型別
# input(): 要求用戶必須輸入一個明確的資料型別,輸入的是什么型別,就存成什么型別
# username = input('請輸入您的賬號:') # hah
# print(username, type(username)) # hah <class 'str'>
#
# age = input("請輸入的你的年齡: ") # age="18"
# print(age, type(age)) # 18 <class 'str'>
# age = int(age) # int只能將純數字的字串轉成整型
# print(age, type(age)) # 18 <class 'int'>
# 2.字串格式化輸出
# 2.1 %
name = '阿旺'
age = 19
print('名字是%s,年齡是%s' % (name, age)) # 只能是元組不能是串列
# %d
print('%5d'%1.5)
# %f
# 2.2 format()---固定的 {}
# 2.2.1順序填坑
# 可以有元素多,不能有元素少!
print('名字是 {},年齡是 {}'.format(name ,age))
# 2.2.2下標填坑
# 不能下標越界 IndexError: tuple index out of range
print('名字是 {1},年齡是 {0}'.format(name ,age))
# 2.2.3變數方法
print('名字是 {name},年齡是 {age}'.format(name='tom' ,age = 18))
# 2.3.4指定長度輸出:
# 1- {:長度}
# 1- 數值型:右對齊,左補齊
# 2- 字串:左對齊,右補齊
# 2- > 右對齊
# 3- < 左對齊
# 4- ^ 中間對齊 ---異或
# 5- 數值補0 ,一般是右對齊 , 左補0 ,不改變值
# 6- 字串本身帶花括號 {{}}
# 2.3 f''
print(f'名字是{name},年齡是{age}')
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/241220.html
標籤:Python
上一篇:04-垃圾回識訓制
下一篇:06-運算子
