Skip to main content
 首页 » 编程设计

Python 更改字符串的打印方式

2025年05月04日20itcoder

我从另一个程序输出一个字符串,显示日期为

16/05/03 # (YY/MM/DD) 

我想把它改成

03/05/16 #(DD/MM/YY) 

这是提供日期的方式

(date = info[4].replace('"', '') 

我试过了

dates = str(date)[::-1] 

但这给了我

的输出
 40/50/61 

不是我想要的

任何使用最少代码的想法?

请您参考如下方法:

>>> '/'.join('16/05/03'.split('/')[::-1]) 
'03/05/16' 

或

>>> '/'.join(reversed('16/05/03'.split('/'))) 
'03/05/16' 

或使用 datetime 库:

>> from datetime import datetime 
>>> datetime.strftime(datetime.strptime('16/05/03', '%y/%m/%d'), '%d/%m/%y') 
'03/05/16'