Python文本处理

大写转换

大写转换详细操作教程
大写字符串是任何文本处理系统中的常规需求。 Python通过使用标准库中的内置函数实现了它。 在下面的例子中,我们使用两个字符串函数capwords()和upper()来实现这一点。'capwords'将每个单词的第一个字母大写,而'upper'将整个字符串大写。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-23
import string
text = 'test - simple easy learning.'
print string.capwords(text)
print string.upper(text)
当运行上面的程序时,得到以下输出 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-23
test - Simple Easy Learning.
LIDIHUO - SIMPLE EASY LEARNING.
Python中的转换本质上意味着用另一个字母替换特定字母。 它可以用于字符串的加密解密。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-23
import string
text = 'test - simple easy learning.'
transtable = string.maketrans('tpol', 'wxyz')
print text.translate(transtable)
当运行上面的程序时,我们得到以下输出 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-23
Tuwyriazsxyinw - simxze easy zearning.
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4