总结

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

作者:CoderZhCoderZh的技术博客 - 博客园

出处:http://coderzh.cnblogs.com/

python 字符串 大小写转换的更多相关文章

  1. 【转】Python 字符串大小写转换

    转载自:python 中字符串大小写转换 一.pyhton字符串的大小写转换, 常用的有以下几种方法: 1.对字符串中所有字符(仅对字母有效)的大小写转换,有两个方法: print 'just to ...

  2. python 字符串大小写转换(不能使用swapcase()方法)

    python 3字符串大小写转换 要求不能使用swapcase()方法 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wa ...

  3. python 字符串 大小写转换 以及一系列字符串操作技巧

    总结 capitalize() 首字母大写,其余全部小写 upper() 全转换成大写 lower() 全转换成小写 title() 标题首字大写,如"i love python" ...

  4. Python 字符串大小写转换

    以下代码演示了如何将字符串转换为大写字母,或者将字符串转为小写字母等: # Filename : test.py # author by : www.runoob.com str = "ww ...

  5. python字符串大小写转换

    str = "www.w3cSChool.cn"print(str.upper()) # 把所有字符中的小写字母转换成大写字母print(str.lower()) # 把所有字符中 ...

  6. linux bash shell:最方便的字符串大小写转换(lowercase/uppercase conversion) (转)

    原文地址:https://blog.csdn.net/10km/article/details/83384145 关于字符串大小写转换,是写 linux 脚本经常干的事儿,所以总想找个方便的方法让我少 ...

  7. [Swift]字符串大小写转换,同时实现本地化或设置语言环境

    在NSString中提供了3种字符串大小写转换方式:1. 转换字符串大小写2. 转换字符串大小写,并实现本地化3. 转换字符串大小写,并设置语言环境. 一. 转换字符串大小写如果只是想单纯的将字符串进 ...

  8. boost 字符串大小写转换

    示例代码如下: #include <boost/algorithm/algorithm.hpp> #include <iostream> using namespace std ...

  9. java字符串大小写转换的两种方法

    转载自:飞扬青春sina blogjava字符串大小写转换的两种方法 import java.io..* public class convertToPrintString {          pu ...

随机推荐

  1. SharePoint 2013 开发——概述

     博客地址:http://blog.csdn.net/FoxDave 近来阅读SharePoint 2013开发一书,带着与大家一起分享其中的内容. 部署场景: 本地部署(On-Premise D ...

  2. hadoop2.x通过Zookeeper来实现namenode的HA方案以及ResourceManager单点故障的解决方案

    我们知道hadoop1.x之前的namenode存在两个主要的问题:1.namenode内存瓶颈的问题,2.namenode的单点故障的问题.针对这两个问题,hadoop2.x都对它进行改进和解决.其 ...

  3. python随笔

    1. 使用iter实现接收用户多行输入 stopword = '' str = '' print('请将要添加的内容输入下方,输入空白行按回车退出程序:') for line in iter(inpu ...

  4. Java Serializable

    实现Serializable的class表明object可以被保存. 被保存的时候实际是存储class里的instance variable,这样在deserialization的时候可以恢复obje ...

  5. Euro Efficiency_完全背包

    Description On January 1st 2002, The Netherlands, and several other European countries abandoned the ...

  6. Fix VNC Desktop Sharing on Ubuntu Desktop 14.04

    Solution 1 sudo apt-get -y install dconf-tools dconf write /org/gnome/desktop/remote-access/require- ...

  7. python模块的安装

    1.下载所需模块 2.解压到一个目录 3.window下打开cmd 4.切换到模块setup.py目录 5.执行python setup.py install安装 前提是安装了python,并且配置了 ...

  8. 【avalon源码】

    1. document.getElementsByTagName('head')[0] document.head 2. 3. var IEVersion = NaN if (window.VBArr ...

  9. 给linux添加一个回收站

    http://blog.chinaunix.net/uid-26805356-id-3492419.html 都知道linux没有回收站,如果一不小心 rm -rf之后,很难恢复,所以就编写了一个回收 ...

  10. 浅谈github页面域名绑定

    来源:http://yanping.me/cn/blog/2011/12/04/github-pages-domain/ 前段时间看到COS上的各位都有博客,也想开个博,给COS的各位管理员发邮件,向 ...