Python字符串大小写转换拼接删除空白

yipeiwu_com6年前Python基础

1.字符串大小写转换

  • string.title() #将字符串中所有单词的首字母以大写形式显示
  • string.upper() #将字符串中所有字母转化为大写字母
  • string.lower() #将字符串中所有字母转化为小写字母
str = "hello world!"
print(str.title())
Hello World!
print(str.upper())
HELLO WORLD!
print(str.lower())
hello world!

2.字符拼接

python中只用使用'+'便可以进行字符串拼接

str1 = 'Hello'
str2 = 'World'
print(str1+str2)
HelloWorld
str3 = str1+str2
print(str3)
HelloWorld

3.删除空白

string.lstrip() #删除串首的空白
string.rstrip() #删除串尾的空白
string.strip() #删除左右量表的空白

strip的意思就是剥夺,所以lstrip()就是剥夺left_strip,剥除左边的数组即串首的空白

str = ' Hello Beijing '
print(str.lstrip())
print(str.rstrip())
print(str.strip())
Hello Beijing 
 Hello Beijing
Hello Beijing

总结

以上所述是小编给大家介绍的Python字符串大小写转换拼接删除空白,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

django 删除数据库表后重新同步的方法

django 删除数据库表后重新同步的方法

由于项目需要,最近在用基于Python语言的一个后端框架Django开发web应用。不得不说,Django继承了Python的简洁性,用它来开发web应用简单清爽,不同于从前的SSH框架...

Python通过正则表达式选取callback的方法

本文实例讲述了Python通过正则表达式选取callback的方法。分享给大家供大家参考。具体如下: 最近在瞎想怎么通过xpath去精确抓取文章的正文,跟parselets类似的想法,只...

pandas中DataFrame修改index、columns名的方法示例

一般常用的有两个方法: 1、使用DataFrame.index = [newName],DataFrame.columns = [newName],这两种方法可以轻松实现。 2、使用...

Python Queue模块详解

Python中,队列是线程间最常用的交换数据的形式。Queue模块是提供队列操作的模块,虽然简单易用,但是不小心的话,还是会出现一些意外。 创建一个“队列”对象 import Queue...

Python 实现异步调用函数的示例讲解

async_call.py #coding:utf-8 from threading import Thread def async_call(fn): def wrapper...