Python字符串与正则表达式
原创
©著作权归作者所有:来自51CTO博客作者JDSH0224的原创作品,请联系作者获取转载授权,否则将追究法律责任
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 字符串的截取
格式:
源码:
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模块处理正则表达式