2007-06-18
Python Cookbook 1.12 转换单词的大小写
需求:
转换单词的大小写.
讨论:
string的upper和lower方法等实现这样的功能.它们都不带参数,返回转换后字符串的副本.
如:
>>>print 'BIG'.lower()
big
>>>print 'little'.upper()
LITTLE
如果要单词的首字母大写,其余字母小写,要使用capitalize方法.而如果要一个字符串中几个单词的首字母都大写,要使用title方法.如:
>>>print 'hello world'.captalize()
Hello world
>>>print 'hello world'.title()
Hello World
大小写控制是字符串处理中经常遇到的需求,所以提供了几个处理大小写的方法.另外,也提供了一些判断大小写的方法:
islower,isupper和istitle,可是没有提供iscaptalize方法,不过我们可以自己来实现一个简单版本:
def iscaptalize(s):
return s==s.captalize()
这个方法有一个缺陷, 不能处理空字符或者空白字符.下面是一个比较严格的版本:
import string
notrans = string.maketrans ('', '') # identity "translation"
def containsAny(str, strset):
return len(strset) != len(strset.translate(notrans, str))
def iscapitalized(s):
return s == s.capitalize( ) and containsAny(s, string.letters)
其中的containsAny方法,用于判断在字符串str中,是否包含指定字符集strset中的字符.使用string.letters来获得字母.这样就能处理空字符和空白字符的情况了.
相关说明;
lower(...)
S.lower() -> string
Return a copy of the string S converted to lowercase.
upper(...)
S.upper() -> string
Return a copy of the string S converted to uppercase.
title(...)
S.title() -> string
Return a titlecased version of S, i.e. words start with uppercase
characters, all remaining cased characters have lowercase.
islower(...)
S.islower() -> bool
Return True if all cased characters in S are lowercase and there is
at least one cased character in S, False otherwis
isupper(...)
S.isupper() -> bool
Return True if all cased characters in S are uppercase and there is
at least one cased character in S, False otherwise.
istitle(...)
S.istitle() -> bool
Return True if S is a titlecased string and there is at least one
character in S, i.e. uppercase characters may only follow uncased
characters and lowercase characters only cased ones. Return False
otherwise.
转换单词的大小写.
讨论:
string的upper和lower方法等实现这样的功能.它们都不带参数,返回转换后字符串的副本.
如:
>>>print 'BIG'.lower()
big
>>>print 'little'.upper()
LITTLE
如果要单词的首字母大写,其余字母小写,要使用capitalize方法.而如果要一个字符串中几个单词的首字母都大写,要使用title方法.如:
>>>print 'hello world'.captalize()
Hello world
>>>print 'hello world'.title()
Hello World
大小写控制是字符串处理中经常遇到的需求,所以提供了几个处理大小写的方法.另外,也提供了一些判断大小写的方法:
islower,isupper和istitle,可是没有提供iscaptalize方法,不过我们可以自己来实现一个简单版本:
def iscaptalize(s):
return s==s.captalize()
这个方法有一个缺陷, 不能处理空字符或者空白字符.下面是一个比较严格的版本:
import string
notrans = string.maketrans ('', '') # identity "translation"
def containsAny(str, strset):
return len(strset) != len(strset.translate(notrans, str))
def iscapitalized(s):
return s == s.capitalize( ) and containsAny(s, string.letters)
其中的containsAny方法,用于判断在字符串str中,是否包含指定字符集strset中的字符.使用string.letters来获得字母.这样就能处理空字符和空白字符的情况了.
相关说明;
lower(...)
S.lower() -> string
Return a copy of the string S converted to lowercase.
upper(...)
S.upper() -> string
Return a copy of the string S converted to uppercase.
title(...)
S.title() -> string
Return a titlecased version of S, i.e. words start with uppercase
characters, all remaining cased characters have lowercase.
islower(...)
S.islower() -> bool
Return True if all cased characters in S are lowercase and there is
at least one cased character in S, False otherwis
isupper(...)
S.isupper() -> bool
Return True if all cased characters in S are uppercase and there is
at least one cased character in S, False otherwise.
istitle(...)
S.istitle() -> bool
Return True if S is a titlecased string and there is at least one
character in S, i.e. uppercase characters may only follow uncased
characters and lowercase characters only cased ones. Return False
otherwise.
标签: Python