python学习入门之字符串

python学习入门之字符串

第一行python代码

message = 'hello python world'
print(message)
message = 'I love Lina'
print(message)

使用title函数首字母大写的方式显示每一个单词

name = "i lovE linA"
print(name.title())

使用upper函数将字符串全部改为大写

up_str = "i love lina"
print(up_str.upper())

使用lower函数将字符串全部改为小写

low_str = "I Love Lina"
print(low_str.lower())

使用‘+’拼接字符串

first_name = "Wang"
last_name = "Lina"
full_name = first_name + " " + last_name
message = "Hello " + full_name + " !"
print(message)

使用rstrip函数删除字符串结尾多余的空白


space_str = "   I Love Lina   "
print(space_str.rstrip())

使用lstrip函数删除字符串开头多余的空白

print(space_str.lstrip())

使用strip函数删除字符串两端多余的空白

print(space_str.strip())

使用str函数避免类型错误

age = 23
message = "Happy " + str(age) + "rd birthday"
print(message)

猜你喜欢

转载自blog.csdn.net/fzx1123/article/details/86099263