使用函数实现title方法
#定义函数def sen(strs):
#定义接受转换后的值的字符串
newstr = ''
#判断是否为空,为空终止函数;不为空首字母转为大写
if strs != '' and isinstance(strs,str) :
s = ord(strs[0]) - 32
newstr += chr(s)
else:
print('mdzz')
return
#遍历除了首位的其他字母
for i in range(1,len(strs)):
#如果当前字母不为空,且上一位为空将其转换为大写
if strs[i-1] == ' 'and strs[i] != ' ':
sc = ord(strs[i]) - 32
sd = chr(sc)
newstr += sd
else:
#其他字符直接原样添加到字符串
newstr += strs[i]
print(newstr)
return newstr
sen('can you help me?')
Python学习交流、资源共享群:563626388 QQ