Python 大写和翻译


大写字符串是任何文本处理系统中的常规需求。 Python 通过使用标准库中的内置函数来实现它。在下面的示例中,我们使用了两个字符串函数, 大写字() and upper() 为达到这个。 'capwords' 将每个单词的第一个字母大写,而 'upper' 则将整个字符串大写。

import string

text = 'Newbiego - simple easy learning.'

print string.capwords(text)
print string.upper(text)

当我们运行上述程序时,我们得到以下输出:

Newbiego - Simple Easy Learning.
NEWBIEGO - SIMPLE EASY LEARNING.

python中的翻译本质上意味着用另一个字母替换特定字母。它可以用于字符串的加密解密。

import string

text = 'Newbiego - simple easy learning.'

transtable = string.maketrans('tpol', 'wxyz')
print text.translate(transtable)

当我们运行上述程序时,我们得到以下输出:

Tuwyriazsxyinw - simxze easy zearning.