站內搜尋:Yahoo搜尋如果沒有給完整的網址,請在站內再搜尋一次!

2019-08-05

Python : 字串串接的方法

import datetime as dt
zName='Huang'
zNow3=dt.datetime.today().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
nPrice=15
nQty=24
zPrint=''
zHi='Hi!!! '
zNow=', it is '
##字串直接相加串接
zPrint=zPrint+zHi+zName+zNow+zNow3
print(zPrint)    #Hi!!! Huang, it is 2019-08-05 21:00:16.658

#使用join()串接串列中的字串
zPrint=''
aString=['Hi!!! ','Huang',', it is ','2019-08-05 21:00:16.658']
zPrint=zPrint.join(aString)
print(zPrint)    #Hi!!! Huang, it is 2019-08-05 21:00:16.658

#使用%格式化字串帶入變數
zPrint="Hi!!! %s, it is %s. %dx%d=%d"
zPrint=zPrint%(zName, zNow3, nPrice, nQty, nPrice*nQty)
print(zPrint)    #Hi!!! Huang, it is 2019-08-05 21:00:16.658. 15x24=360

沒有留言:

張貼留言