Python 精巧知识点备忘

本文详细介绍了如何在Python中进行字符串操作,包括提取字符、转换类型、串联字符串及使用字符串格式化输出。同时,展示了如何利用datetime库获取当前日期和时间,并以不同格式进行打印。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

字符串的操作

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

    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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值