python 字符串 大小写转换 以及一系列字符串操作技巧
总结
capitalize() 首字母大写,其余全部小写
upper() 全转换成大写
lower() 全转换成小写
title() 标题首字大写,如"i love python".title() "I Love Python"
转换大小写
和其他语言一样,Python为string对象提供了转换大小写的方法:upper() 和 lower()。还不止这些,Python还为我们提供了首字母大写,其余小写的capitalize()方法,以及所有单词首字母大写,其余小写的title()方法。函数较简单,看下面的例子:
s = 'hEllo pYthon'
print s.upper()
print s.lower()
print s.capitalize()
print s.title()
输出结果:
HELLO PYTHON
hello python
Hello python
Hello Python
判断大小写
Python提供了isupper(),islower(),istitle()方法用来判断字符串的大小写。注意的是:
1. 没有提供 iscapitalize()方法,下面我们会自己实现,至于为什么Python没有为我们实现,就不得而知了。
2. 如果对空字符串使用isupper(),islower(),istitle(),返回的结果都为False。
print 'A'.isupper() #True
print 'A'.islower() #False
print 'Python Is So Good'.istitle() #True
#print 'Dont do that!'.iscapitalize() #错误,不存在iscapitalize()方法
实现iscapitalize
1. 如果我们只是简单比较原字符串与进行了capitallize()转换的字符串的话,如果我们传入的原字符串为空字符串的话,返回结果会为True,这不符合我们上面提到的第2点。
def iscapitalized(s):
return s == s.capitalize( )
有人想到返回时加入条件,判断len(s)>0,其实这样是有问题的,因为当我们调用iscapitalize('123')时,返回的是True,不是我们预期的结果。
2. 因此,我们回忆起了之前的translate方法,去判断字符串是否包含任何英文字母。实现如下:
import string
notrans = string.maketrans('', '')
def containsAny(str, strset):
return len(strset) != len(strset.translate(notrans, str))
def iscapitalized(s):
return s == s.capitalize( ) and containsAny(s, string.letters)
#return s == s.capitalize( ) and len(s) > 0 #如果s为数字组成的字符串,这个方法将行不通
调用一下试试:
print iscapitalized('123')
print iscapitalized('')
print iscapitalized('Evergreen is zcr1985')
输出结果:
False
False
True
取出字符串中包含的数字

python 字符串 大小写转换 以及一系列字符串操作技巧的更多相关文章
- 【转】Python 字符串大小写转换
转载自:python 中字符串大小写转换 一.pyhton字符串的大小写转换, 常用的有以下几种方法: 1.对字符串中所有字符(仅对字母有效)的大小写转换,有两个方法: print 'just to ...
- python 字符串大小写转换(不能使用swapcase()方法)
python 3字符串大小写转换 要求不能使用swapcase()方法 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wa ...
- linux bash shell:最方便的字符串大小写转换(lowercase/uppercase conversion) (转)
原文地址:https://blog.csdn.net/10km/article/details/83384145 关于字符串大小写转换,是写 linux 脚本经常干的事儿,所以总想找个方便的方法让我少 ...
- [Swift]字符串大小写转换,同时实现本地化或设置语言环境
在NSString中提供了3种字符串大小写转换方式:1. 转换字符串大小写2. 转换字符串大小写,并实现本地化3. 转换字符串大小写,并设置语言环境. 一. 转换字符串大小写如果只是想单纯的将字符串进 ...
- boost 字符串大小写转换
示例代码如下: #include <boost/algorithm/algorithm.hpp> #include <iostream> using namespace std ...
- java字符串大小写转换的两种方法
转载自:飞扬青春sina blogjava字符串大小写转换的两种方法 import java.io..* public class convertToPrintString { pu ...
- python 字符串 大小写转换
总结 capitalize() 首字母大写,其余全部小写 upper() 全转换成大写 lower() 全转换成小写 title() 标题首字大写,如"i love python" ...
- std::string 字符串大小写转换(转)
该问题归结为std::transform函数的使用 函数原型 template < class InputIterator, class OutputIterator, class UnaryO ...
- strtolower() strtoupper()等字符串大小写转换函数
$str = "Mary Had A Little Lamb and She LOVED It So"; string strtolower ( string $str )— 将字 ...
随机推荐
- ORA-22828 输入样式或替换參数超过了32k限制大小
今天调试程序报下面错误: ORA-22828: input pattern or replacement parameters exceed 32K size limit 22828. 00000 - ...
- 21、java中和日期相关的类
一.Data及其常用API 1.简介 Java中的时间使用标准类库的java.util.Date,其表示特定的瞬间,精确到毫秒.是用距离一个固定时间点的毫秒数(可正可负,long类型)表达一个特定的时 ...
- Tomcat 签名认证配置简例
在项目的web.xml中增加 <security-constraint> <web-resource-collection> <web-resource-name> ...
- ListView中Button事件
为了解决ListView中Item里的Button独立事件响应,能够採用下面方法: 在BaseAdapter的getview里加入加粗代码: <span style="font-siz ...
- jquery实现 批量右移
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- Spring配置文件头信息
代码如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...
- HDUOJ--8球胜负
8球胜负 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- PostgreSQL安装详细步骤(windows)[转]
PostgreSQL安装: 一.windows下安装过程 安装介质:postgresql-9.1.3-1-windows.exe(46M),安装过程非常简单,过程如下: 1.开始安装: 2.选择程序安 ...
- android语音识别方法
http://www.apkbus.com/forum.php?mod=viewthread&tid=3473 android语音识别方法一:使用intent调用语音识别程序 1. 说明 以下 ...
- 推荐系统学习07-Waffles
介绍 Waffles 英文原意是蜂蜜甜饼,在这里却指代一个很强大的机器学习的开源工具包. Waffles里包括的算法特别多.涉及机器学习的方方面面,推荐系统位于当中的Waffles_recommend ...