Python字符串与正则表达式

本文详细介绍了字符串操作和正则表达式的使用方法,包括字符串格式化、转义符、合并、截取、反转、查找与替换,以及日期转换等。同时,深入探讨了正则表达式的特殊字符和re模块的应用。

Table of Contents

1. 字符串

1.1 字符串格式化

1.2 字符串的转义符

1.3 字符串的合并

1.4 字符串的截取

1.5 字符串的反转

1.6 字符串的查找与替换

1.7 字符串与日期的转换

2. 正则表达式

2.1 特殊字符

2.2 re模块处理正则表达式


1. 字符串

1.1 字符串格式化

 

1.2 字符串的转义符

 

1.3 字符串的合并

 

1.4 字符串的截取

格式:

string[start:end:step]

源码:

str = "hello world"
print(str)
print(str[0 : 3])
print(str[::3])
C:\Python\Python38\python.exe "E:/python/python project/hello world.py"
hello world
hel
hlwl

1.5 字符串的反转

 

1.6 字符串的查找与替换

 

1.7 字符串与日期的转换

"""
时间到字符串的转换
strftime(format[, tuple])->string

字符串到时间的转换(2次转换)
strptime(string, format)->struct_time
datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])
"""

源码:

#时间、日期
import time, datetime

#时间到字符串的转换
str = time.strftime("%Y-%m-%d %X", time.localtime())
print(str)

#字符串到时间的转换(2次转换)
t = time.strptime(str, "%Y-%m-%d %H:%M:%S")
Y,M,D,h,m,s = t[0:6]
print(datetime.datetime(Y,M,D,h,m,s))
#print(datetime.datetime(t))

结果:

C:\Python\Python38\python.exe "E:/python/python project/hello world.py"
2019-12-30 23:07:45
2019-12-30 23:07:45
   December 2019
Mo Tu We Th Fr Sa Su
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

2. 正则表达式

2.1 特殊字符

2.2 re模块处理正则表达式

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值