python字符串相关方法
1.修改字符串的大小写
- title()以首字母大写的方式显示每个单词
- upper()将字符串全部改为大写
- lower()将字符串全部改为小写
1 name="ada lovelace" 2 print(name.title()) 3 print(name.upper()) 4 print(name.lower())
Ada Lovelace
ADA LOVELACE
ada lovelace
2.通过+ 合并字符串
first_name="ada"; last_name="lovelance" full_name=first_name+" "+last_name print(full_name)
ada lovelance
3.使用制表符或者换行符来添加空白
>>> print("\tPython") Python >>> print("python") python >>> print("Languages:\nPython\nC#\njava") Languages: Python C# java >>> print("Languages:\n\tPython\n\tC#\n\tJava") Languages: Python C# Java >>>
4.删除空白
lstrip() 删除左边空格
rstrip() 删除右边空格
>>> favorite_language='python ' >>> favorite_language 'python ' >>> favorite_language.rstrip() 'python' >>> favorite_language 'python ' >>> favorite_language=" Python" >>> favorite_language.lstrip() 'Python' >>>