美文网首页
Python>字符串>大小写字母相关函数

Python>字符串>大小写字母相关函数

作者: Manegga | 来源:发表于2017-08-31 23:44 被阅读0次

1. lower()

python自带解释:

 |  lower(...)
 |      S.lower() -> str
 |      
 |      Return a copy of the string S converted to lowercase.

举个栗子:

>>>s = 'Hello World!'
>>>print(s.lower())
hello world!

2. upper()

python自带解释:

 |  upper(...)
 |      S.upper() -> str
 |      
 |      Return a copy of S converted to uppercase.

举个栗子:

>>>s = 'Hello World!'
>>>print(s.upper())
HELLO WORLD!

3. swapcase()

python自带解释:

 |  swapcase(...)
 |      S.swapcase() -> str
 |      
 |      Return a copy of S with uppercase characters converted to lowercase
 |      and vice versa.

举个栗子:

>>>s = 'Hello World!'
>>>print(s.swapcase())
hELLO wORLD!

4. title()

python自带解释:

title(...)
 |      S.title() -> str
 |      
 |      Return a titlecased version of S, i.e. words start with title case
 |      characters, all remaining cased characters have lower case.

举个栗子:

>>>s = 'I love you!'
>>>print(s.title())
I Love You!

5. capitalize()

python自带解释:

 |  capitalize(...)
 |      S.capitalize() -> str
 |      
 |      Return a capitalized version of S, i.e. make the first character
 |      have upper case and the rest lower case.

举个栗子:

>>>s1 = 'Hello World!'
>>>s2 = 'I love you!'
>>>print(s1.capitalize())
Hello world!
>>>pirnt(s2.capitalize())
I love you!

总结

输入

s = 'I love you!'
print(s.upper())
print(s.lower())
print(s.capitalize())
print(s.swapcase())
print(s.title())

输出

I LOVE YOU!
i love you!
I love you!
i LOVE YOU!
I Love You!

相关文章

网友评论

      本文标题:Python>字符串>大小写字母相关函数

      本文链接:https://www.haomeiwen.com/subject/omacjxtx.html