Recipe 1.12. Controlling Case(Python Cookbook)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 >>> 'one tWo thrEe'.capitalize()
2 'One two three'
3 >>> 'hello sunshine'.title()
4 'Hello Sunshine'
5 >>> "abc".upper()
6 'ABC'
7 >>> "RAY".lower()
8 'ray'
9 >>> import string
10 >>> notrans = string.maketrans('', '')
11 >>> def containsAny(str, strset):
12 return len(strset) != len(strset.translate(notrans, str))
13
14 >>> def iscapitalized(s):
15 return s == s.capitalize() and containsAny(s, string.letters)
16
17 >>> iscapitalized('')
18 False
19 >>> iscapitalized('Hello')
20 True
21 >>> iscapitalized('hello')
22 False
2 'One two three'
3 >>> 'hello sunshine'.title()
4 'Hello Sunshine'
5 >>> "abc".upper()
6 'ABC'
7 >>> "RAY".lower()
8 'ray'
9 >>> import string
10 >>> notrans = string.maketrans('', '')
11 >>> def containsAny(str, strset):
12 return len(strset) != len(strset.translate(notrans, str))
13
14 >>> def iscapitalized(s):
15 return s == s.capitalize() and containsAny(s, string.letters)
16
17 >>> iscapitalized('')
18 False
19 >>> iscapitalized('Hello')
20 True
21 >>> iscapitalized('hello')
22 False