码迷,mamicode.com
首页 > 编程语言 > 详细

Python - 缩写(capwords) 和 创建转换表(maketrans) 详细说明

时间:2015-09-28 20:51:36      阅读:636      评论:0      收藏:0      [点我收藏+]

标签:

缩写(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 string

s = ‘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.


技术分享





版权声明:本文博主原创文章。博客,未经同意不得转载。

Python - 缩写(capwords) 和 创建转换表(maketrans) 详细说明

标签:

原文地址:http://www.cnblogs.com/bhlsheji/p/4844859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!