Python Cute Case _python

Source: Internet
Author: User
function is simpler, look at the following example:
Copy Code code as follows:

s = ' HEllo PYthon '
Print S.upper ()
Print S.lower ()
Print S.capitalize ()
Print S.title ()

Output results:
HELLO PYTHON
Hello python
Hello python
Hello Python


Judge case
Python provides isupper (), Islower (), and Istitle () methods to determine the case of strings. Note that:
1. No Iscapitalize () method is provided, we will implement it ourselves, and it is not known why Python has not been implemented for us.
2. If you use Isupper () for an empty string, Islower (), Istitle (), the result returned is false.
Copy Code code as follows:

print ' A '. Isupper () #True
print ' A '. Islower () #False
print ' Python is so good '. Istitle () #True
#print ' Dont do that! '. Iscapitalize () #错误, no Iscapitalize () method exists


Implement Iscapitalize
1. If we simply compare the original string with the string that was Capitallize (), if the original string we passed in was an empty string, the return result would be true, which does not correspond to the 2nd we mentioned above.
def iscapitalized (s):
return s = = S.capitalize () Some people think of returning to add conditions, to judge Len (s) >0, in fact, this is problematic, because when we call Iscapitalize (' 123 '), return is true, not our expected results.
2. Therefore, we recall the previous translate method to determine whether the string contains any English letters. Implemented as follows:
Copy Code code as follows:

Import string
Notrans = String.maketrans (', ')
def containsany (str, strset):
Return Len (strset)!= len (strset.translate (Notrans, str))
def iscapitalized (s):
return s = = S.capitalize () and Containsany (S, string.letters)
#return s = = S.capitalize () and Len (s) > 0 #如果s为数字组成的字符串, this method will not work. Call a try:
Print iscapitalized (' 123 ')
Print iscapitalized (')
Print iscapitalized (' Evergreen is zcr1985 ')

Output results:
False
False
True

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.