第一章:变量和简单的数据类型

字符串:

使用方法修改字符串的大小写

使用方法.title()改修字符串中每个单词首字母为大写

a= "asd dfsdf"
print(a.title())
执行结果:
Asd Dfsdf

使用+拼接字符串

a = "shen"
b = "yile"
c = a + " " + b
print(c.title())
执行结果:
Shen Yile

使用制表符\t和换行符\n添加空格

a = "shen"
b = "yi\tle"
c = a + "\n" + b
print(c.title())
执行结果:
Shen
Yi    Le

使用方法rstrip、lstrip、strip删除末尾、开头、和开头结尾空格

疑问:字符串中间的空格怎么去除?

使用str()函数避免类型错误

age = 23
message = "Happy " + str(age) + "rd Birthday!"
print(message)
执行结果:
Happy 23rd Birthday!

猜你喜欢

转载自www.cnblogs.com/sxdpython/p/12591691.html