Python | 带有示例的string.upper(),string.lower()和string.title()方法

string.upper(),和的方法是在Python内置方法,这些被用来格式字符串在像转换为大写,小写或小的情况下格式的特定格式。string.lower()string.title()

1) string.upper()

方法返回大写字符串(字符串的所有字符均为大写)。

示例: “ HELLO WORLD”

2) string.lower()

方法返回小写字符串(字符串中的所有字符均小写)。

示例: “ hello world”

3) string.upper()

方法返回标题大小写字符串(每个单词的第一个字符为大写,其余所有字符为小写)。

示例: “ Hello World”

语法:

    string.upper()
    string.lower()
    string.title()

范例1:

# 声明一个字符串 
str = "HELLO World How are you?"

# 大写字符串 
print "Uppercase string: ",str.upper()

# 小写字符串
print "Lowercase string: ",str.lower()

# 标题大小写字符串
print "Title case string: ",str.title()

输出结果

    Uppercase string:  HELLO WORLD HOW ARE YOU?
    Lowercase string:  hello world how are you?
    Title case string:  Hello World How Are You?