折騰了一天,一直在用CMD學習Python寫Hello World。偶然間發現能夠用Pycharm。也算是給後面想學習的人提個醒,方便省事許多。python
format()使用方法。oop
age = 20
name = 'Swaroop'
print('{0} was {1} years old when he wrote this book'.format(name, age))
print('Why is {0} playing with that python?'.format(name))
學習
# 對於浮點數 '0.333' 保留小數點(.)後三位
print('{0:.3f}'.format(1.0/3))
# 使用下劃線填充文本, 並保持文字處於中間位置
# 使用 (^) 定義 '___hello___'字符串長度爲 11
print('{0:_^11}'.format('hello'))
# 基於關鍵詞輸出 'Swaroop wrote A Byte of Python'
print('{name} wrote {book}'.format(name='Swaroop', book='A Byte of Python'))
this
輸出:
0.333
___hello___
Swaroop wrote A Byte of Pythonspa
佔位符 | 替換內容 |
---|---|
%d | 整數 |
%f | 浮點數 |
%s | 字符串 |
%x | 十六進制整數 |
指定以空格結尾,end=’‘orm
continue 語句,跳過當前循環塊中的剩餘語句,並開始下一次迭代。字符串