Python - 首字母大写(capwords) 和 创建转换表(maketrans) 详解

来源:互联网 发布:淘宝24小时投诉电话 编辑:程序博客网 时间:2024/05/19 09:49

首字母大写(capwords) 和 创建转换表(maketrans) 详解


本文地址: http://blog.csdn.net/caroline_wendy/article/details/27051977


string模块, 包含处理文本的常量和类.

capwords(), 把字符串(string)中, 所有单词的首字母大写;

maketrans(), 创建对照表, 然后使用translate()函数, 调用对照表, 把字符串(string)中的字符, 进行相应的替换.


代码:

# -*- coding: utf-8 -*-'''Created on 2014.5.26@author: C.L.Wang'''import strings = 'The quick brown fox jumped over the lazy dog.'leet = string.maketrans('abcdefg', '1234567') #leet 脑残if __name__ == '__main__':    print(s)    print(string.capwords(s)) #首字母大写    print(s.translate(leet)) #转换    pass

输出:

The quick brown fox jumped over the lazy dog.The Quick Brown Fox Jumped Over The Lazy Dog.Th5 qui3k 2rown 6ox jump54 ov5r th5 l1zy 4o7.







8 0
原创粉丝点击