字符串处理入门

Python 中的正则表达式

Maria Eugenia Inzaugarat

Data Scientist

您将学习

  • 字符串操作

    • 如替换、查找子串
  • 字符串格式化

    • 如在模板中插入字符串
  • 基础与高级正则表达式

    • 如在字符串中匹配复杂模式
Python 中的正则表达式

重要性

  • 清洗数据集,为文本挖掘或情感分析做准备

  • 处理电子邮件内容,供机器学习算法判断是否为垃圾邮件

  • 解析并提取网站中的特定数据以构建数据库

Python 中的正则表达式

字符串

  • 字符序列

  • 引号

my_string = "This is a string"
my_string2 = 'This is also a string'
my_string = 'And this? It's the wrong string'
my_string = "And this? It's the correct string"
Python 中的正则表达式

更多字符串操作

  • 长度
my_string = "Awesome day"

len(my_string)
11
  • 转为字符串
str(123)
'123'
Python 中的正则表达式

连接

  • 连接:+ 运算符
my_string1 = "Awesome day"
my_string2 = "for biking"
print(my_string1+" "+my_string2)
Awesome day for biking
Python 中的正则表达式

索引

  • 方括号表示法

my_string = "Awesome day"
print(my_string[3])
s

 

print(my_string[-1])
y
Python 中的正则表达式

切片

  • 方括号表示法

my_string = "Awesome day"
print(my_string[0:3])
Awe

 

print(my_string[:5])
print(my_string[5:])
Aweso
me day
Python 中的正则表达式

步长

  • 指定步长

my_string = "Awesome day"
print(my_string[0:6:2])
Aeo

 

print(my_string[::-1])
yad emosewA
Python 中的正则表达式

Vamos praticar!

Python 中的正则表达式

Preparing Video For Download...