title()
title()方法是将字符串中每个单词以首字母大写的方式显示
如:
message = 'hello python world'
print(message.title())
运行结果:
upper()
upper()方法是将字符串中的字母全部变为大写
如:
message = 'hello python world'
print(message.upper())
运行结果:
lower()
lower()方法是将字符串中的字母全部变为小写
如:
message = 'hello python world'
print(message.lower())
运行结果:
rstrip()
要确保字符串末尾没有空白,可使用rstrip()方法
如:
message = 'hello python world '
print(message + '123')
print(message.rstrip() + '123')
运行结果:
lstrip()
剔除字符串开头的空白:
message = ' hello python world'
print('123' + message)
print('123' + message.lstrip())
运行结果:
strip()
剔除字符串两端的空白:
message = ' hello python world '
print('123' + message + '321')
print('123' + message.strip() + '321')
运行结果: