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。
我是阿旺,明天更新格式化字符串,用微笑告诉别人,今天的你比昨天更坚强。