Python字符串,去空白和大小写切换

删除空白

Python能够找出字符串开头和末尾多余的空白。要删除空白可用rstrip()(删除末尾空白)、lstrip()(删除开通空白)和strip()(删除两边空白)

favorite_languages = " python "
print("\"" + favorite_languages + "\"")
print("\"" + favorite_languages.rstrip() + "\"")
print("\"" + favorite_languages.lstrip() + "\"")
print("\"" + favorite_languages.strip() + "\"")

Python字符串,去空白和大小写切换

字符串大小写切换

title(): 首字母大写
upper(): 全部大写
lower(): 全部小写

name = "Liang chen"
print("小写:" + name.lower())
print("大写:" + name.upper())
print("首字母大写:" + name.title())

Python字符串,去空白和大小写切换

上一篇:LeetCode 389 Find the Difference 解题报告


下一篇:1045 Favorite Color Stripe (30 分)