文章来自《Python cookbook》.

翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!

-- 0.706 [2004-09-04 19:45:31]

1. 操纵大小写

Credit: Luther Blissett

1.1. 问题 Problem

You need to convert a string from uppercase to lowercase, or vice versa.

你需要将一个字符串从大写转换为小写,或者相反.

1.2. 解决 Solution

That's what the upper and lower methods of string objects are for. Each takes no arguments and returns a copy of the string in which each letter has been changed to upper- or lowercase, respectively.

那就是string的方法'upper'和'lower'的用处.它们不需要参数,返回一个原字符串的拷贝--其中的每个字符都被转化为相应的大写或小写.

   1 big = little.upper(  )
   2 little = big.lower(  )

s.capitalize is similar to s[ :1].upper()+s[1:].lower( ). The first character is changed to uppercase, and all others are changed to lowercase. s.title is similar, but it uppercases the first letter of each word:

s.capitalize 类似于s[ :1].upper()+s[1:].lower( ).第一个字符被转化为大写,所有其余字符被转化为小写.s.title也类似,不过它将每一个单词的第一个字符转化为大写:

>>> print 'one two three'.capitalize(  )
One two three
>>> print 'one two three'.title(  )
One Two Three

1.3. 讨论 Discussion

Case manipulation of strings is a very frequent need. Because of this, several string methods let you produce case-altered copies of strings. Moreover, you can also check if a string object is already in a given case form with the methods isupper, islower, and istitle, which all return 1 if the string is nonempty and already meets the uppercase, lowercase, or titlecase constraints. There is no iscapitalized method, but we can code it as a function:

字符串的大小写操作是非常频繁的。因此,有几个字符串方法让你产生字符串的拷贝并改变它们的大小写。此外,你还能够检查是否一个字符串已经是给定的大小写形式。使用isupper,islittle以及istitle方法,如果字符串非空且已经是大写,小写或标题形式(句中单词的第一字符为大写,其余为小写),它们都会返回1。没有iscapitalized方法(字符串中第一字符为大写,其余为小写),但我们可以用以下代码来实现这个函数:

   1 def iscapitalized(s):
   2     return s[:1].isupper() and s[1:].islower(  )

This may not be exactly what you want, because each of the is methods returns 0 for an empty string, and the three case-checking ones also return 0 for strings that, while not empty, contain no letters at all. This iscapitalized function does not quite match these semantics; rather, it accepts a string s only if s starts with an uppercase letter, followed by at least one more character, including at least one more letter somewhere, and all letters except the first one are lowercase. Here's an alternative whose semantics may be easier to understand:

这也许不是你想要的精确结果,因为每个is*方法在字符串为空时返回0,而且三个大小写检查方法在字符串非空但不包含任何字母时也返回0。这个iscapitalized 函数不是很符合这些语义;另外,它只认可那些以大写字母开头,后跟至少1个 以上字符,其中在某处有至少1个以上字母,并且所有字母除第一个外都是小写。这里有一个可选方法,它的语义是容易理解的:

   1 def iscapitalized(s):
   2     return s == s.capitalize(  )

However, this version deviates from the boundary-case semantics of the methods by accepting strings that are empty or contain no letters. Depending on your exact needs for boundary cases, you may of course implement precisely those checks you want to perform.

可是,这个版本偏离了方法的boundary-case语义,因为它认可了那些空的或不含字母的字符串当然,根据你对boundary-case的确切需要,你可以精确的实现那些你想要完成的检查.

1.4. 参考 See Also

The Library Reference section on string methods; Perl Cookbook Recipe 1.9.