string.upper(), string.lower() and string.title() Methods are inbuilt methods in Python, these are used to format string in a particular format like uppercases, lowercase or little case format.
string.upper(),string.lower()和string.title()方法是Python中的内置方法,用于将字符串格式化为特殊格式,例如大写,小写或小写。
1) string.upper()
1)string.upper()
Method returns uppercase string (where all characters of the string are in uppercase).
方法返回大写字符串(其中字符串的所有字符均为大写)。
Example: "HELLO WORLD"
示例: “ HELLO WORLD”
2) string.lower()
2)string.lower()
Method returns lowercase string (where all characters of the string are in lowercase).
方法返回小写字符串(其中字符串的所有字符均小写)。
Example: "hello world"
示例: “ hello world”
3) string.upper()
3)string.upper()
Method returns title case string (where first character of the each words are in uppercase, rest of all characters are in lowercase).
方法返回标题大小写字符串(每个单词的第一个字符为大写,其余所有字符为小写)。
Example: "Hello World"
示例: “ Hello World”
Syntaxes:
语法:
string.upper()
string.lower()
string.title()
Example 1:
范例1:
# declare a string
str = "HELLO World How are you?"
# uppercase string
print "Uppercase string: ",str.upper()
# lowercase string
print "Lowercase string: ",str.lower()
# title case string
print "Title case string: ",str.title()
Output
输出量
Uppercase string: HELLO WORLD HOW ARE YOU?
Lowercase string: hello world how are you?
Title case string: Hello World How Are You?
Example 2: Change string case – if string is in uppercase, convert it to lowercase and if it is in lowercase convert it to uppercase.
示例2:更改字符串大小写–如果字符串为大写,则将其转换为小写,如果为小写,则将其转换为大写。
There is a string, we have to change its case, if string is in uppercase convert it to lowercase. If string is in lowercase convert it to uppercase, otherwise convert string into title case.
有一个字符串,我们必须更改其大小写,如果字符串是大写的,请将其转换为小写。 如果字符串为小写,则将其转换为大写,否则将其转换为标题。
# function to check and change case
def changeCase(str) :
if(str.isupper()):
str = str.lower()
elif(str.islower()):
str = str.upper()
else:
str = str.title()
return str;
# Main code
str = "HELLO WORLD"
print changeCase(str)
str = "hello world"
print changeCase(str)
str = "HELLO world"
print changeCase(str)
Output
输出量
hello world
HELLO WORLD
Hello World