python 字符串 字母大小写转化,去重空格和特殊字符

1:字母大小写

一:lower() 大写转小写,如果字符串中没有符合条件的,就返回原字符串

str1 = 'hello WORLD'
str2 = 'hello world'
print(str1.lower()) # hello world
print(str2.lower()) # hello world

二:upper()小写转大写,和lower()是相反的,语法一样,没有符合条件的,也是将原字符串返回

str1 = 'hello WORLD'
str2 = 'hello world'
print(str1.upper()) # HELLO WORLD
print(str2.upper()) # HELLO WORLD

2:去除字符串中的空格和特殊字符

一:strip()用于去除字符串两端的特殊字符

str1 = ' http://www.mingrisoft.com   \t\r\n'
print '原字符串str1:' + str1 +'。'
print '去除手尾的特殊字符str1:' + str1.strip() + '。'
str2 = '@明日之星.@.'
print '原字符串str2:' + str2 +'。'
print '去除@.的字符串str2:' + str2.strip('@.') + '。'
# 原字符串str1: http://www.mingrisoft.com   	
# 。
# 去除手尾的特殊字符str1:http://www.mingrisoft.com。
# 原字符串str2:@明日之星.@.。
# 去除@.的字符串str2:明日之星。

二:lstrip()去掉字符串左侧的空格和特殊字符

tr1 = '\t\t\t   http://www.mingrisoft.com   \t\r\n'
print '原字符串str1:' + str1 +'。'
print '去除左侧的特殊字符str1:' + str1.lstrip() + '。'
# 原字符串str1:			   http://www.mingrisoft.com   	
# 。
# 去除左侧的特殊字符str1:http://www.mingrisoft.com   	
# 。

三:rstrip()去掉字符串右侧的空格和特殊字符

和lstrip()是相反的,是去掉右侧的空格和特殊字符

str1 = '\t\t\t   http://www.mingrisoft.com   \t\r\n'
print '原字符串str1:' + str1 +'。'
print '去除左侧的特殊字符str1:' + str1.rstrip() + '。'
# 原字符串str1:			   http://www.mingrisoft.com   	
# 。
# 去除左侧的特殊字符str1:			   http://www.mingrisoft.com。

我是阿旺,明天更新格式化字符串,用微笑告诉别人,今天的你比昨天更坚强。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值