The Python programming language can bring us great benefits in practical applications. Today, we will use a basic application technology to explain the basic application methods of this programming language in detail. First, let's take a look at the Python case-sensitive applications here.
- Explanation of the specific application method of the Python submission form
- Python file type
- Python yield usage
- Analysis on the basic operation steps for Python Access Database Operations
- Introduction to Python WEB testing methods
Convert Python case
Like other languages, Python provides string objects with a case-insensitive conversion method: upper () and lower (). More than that, Python also provides us with the capitalize () method in upper-case letters, the capitalize () method in lower-case letters, and the title () method in lower-case letters. The function is relatively simple. See the following example:
- s = 'hEllo pYthon'
- print s.upper()
- print s.lower()
- print s.capitalize()
- print s.title()
Output result:
- HELLO PYTHON
- hello python
- Hello python
- Hello Python
Determine the case sensitivity of Python
Python provides the isupper (), islower (), and istitle () methods to determine the case sensitivity of strings. Note:
1. The iscapitalize () method is not provided. We will implement it by ourselves below. It is unknown why Python is not implemented for us.
2. If isupper (), islower (), istitle () is used as the null string, the returned result is False.
- Print 'A'. isupper () # True
- Print 'A'. islower () # False
- Print 'python Is So Good '. istitle () # True
- # Print 'dont do that! '. Iscapitalize () # error. The iscapitalize () method does not exist.
Implement iscapitalize
1. if we simply compare the original string with the string converted by capitallize (), if the input original string is a null string, the return result will be True, this does not match the 2nd point we mentioned above.
- def iscapitalized(s):
- return s == s.capitalize( )
Some people think of adding conditions to return results and determining len (s)> 0. In fact, this is a problem, because when we call iscapitalize ('20140901'), True is returned, not our expected result.
2. Therefore, we recall the previous translate method to determine whether a string contains any English letter. The implementation is 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 # If s is a string composed of numbers,
This method will not work. Try again:
- Print iscapitalized ('20140901 ')
- Print iscapitalized ('')
- Print iscapitalized ('evergreenis zcr1985 ')
Output result:
- False
- False
- True
The above is what we will introduce to you about Python case sensitivity.