您的位置:首页 > 编程语言 > Python开发

Python 精巧知识点备忘

2015-06-04 11:11 441 查看

字符串的操作

取出字符串中的其中一个字符

string = "Python"

#取出第一个字符

first = string[0]

#或者

first = "Python"[0]


转换为字符串

#str()
pi = 3.14
print str(pi)


字符串串联:+

print "Spam "+"and "+"eggs"

#打印一个字符串和变量
string_1 = "Camelot"
string_2 = "place"
print "Let's not go to %s. It\' a silly %s."%(string_1,string_2)

#字符串直接带入
print "The %s who %s %s!" % ("Knights", "say", "Ni")


datetime 库

from datetime import datetime

#备注:打印的不是北京时间
print datetime.now()

#打印部分时间,年或者月或者时间点

from datetime import datetime
now = datetime.now()
print now.year
print now.month
print now.day
print '%s:%s:%s' % (now.year, now.month, now.day)
print '%s/%s/%s' % (now.month, now.day, now.year)
print '%s:%s:%s' % (now.hour, now.minute, now.second)

#打印时间格式如下:mm/dd/yyyy hh:mm:ss
print '%s/%s/%s %s:%s:%s' % (now.month, now.day, now.year,now.hour, now.minute, now.second)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: