Home  >  Article  >  Backend Development  >  How to convert case in python

How to convert case in python

Y2J
Y2JOriginal
2017-05-18 14:54:127131browse

Like other languages, Python provides methods for converting case to string objects: upper() and lower(). That's not all, Python also provides us with the capitalize() method, which capitalizes the first letter of all words and lowercases the rest, and the title() method, which capitalizes the first letter of all words and lowercases the rest.

The function is relatively simple, see the example below:

The code is as follows:

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 case
Python provides isupper(), islower(), istitle() methods to determine the case of string . Note:
1. The iscapitalize() method is not provided. We will implement it ourselves below. As for why Python did not implement it for us, it is unknown.
2. If you use isupper(), islower(), istitle() on an empty string, the returned result will be False.

The code is as follows:

print 'A'.isupper() #True 
print 'A'.islower() #False 
print 'Python Is So Good'.istitle() #True 
#print 'Dont do that!'.iscapitalize() #错误,不存在iscapitalize()方法

Implementing iscapitalize
1. If we simply compare the original string with the string converted by capitalallize(), if we pass in If the original string is an empty string, the return result will be True, which does not comply with point 2 mentioned above.
def iscapitalized(s):
return s == s.capitalize( ) Some people think of adding conditions when returning to judge len(s)>0. In fact, this is problematic because when we call iscapitalize( '123'), the return is True, which is not the result we expected.
2. Therefore, we recall the previous translate method to determine whether the string contains any English letters. The implementation is as follows:

The code is as follows:

import string 
notrans = string.maketrans('', '') 
def containsAny(str, str
set
): 
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为数字组成的字符串,这个方法将行不通调用一下试试: 
print iscapitalized('123') 
print iscapitalized('') 
print iscapitalized('Evergreen is zcr1985')

Output result:

False 
False 
True

[Related recommendations]

1. Python free video tutorial

2. Detailed explanation of the use of lower and upper functions in python

3. Introduction to upper in python basics

The above is the detailed content of How to convert case in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn