总结

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 字符串 大小写转换 以及一系列字符串操作技巧的更多相关文章

  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. linux bash shell:最方便的字符串大小写转换(lowercase/uppercase conversion) (转)

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

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

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

  5. boost 字符串大小写转换

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

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

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

  7. python 字符串 大小写转换

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

  8. std::string 字符串大小写转换(转)

    该问题归结为std::transform函数的使用 函数原型 template < class InputIterator, class OutputIterator, class UnaryO ...

  9. strtolower() strtoupper()等字符串大小写转换函数

    $str = "Mary Had A Little Lamb and She LOVED It So"; string strtolower ( string $str )— 将字 ...

随机推荐

  1. HDU Always Cook Mushroom (极角排序+树状数组)

    Problem Description Matt has a company, Always Cook Mushroom (ACM), which produces high-quality mush ...

  2. oracle 多字段去重查询

      oracle 多字段去重查询 CreationTime--2018年6月29日15点11分 Author:Marydon 1.情景展示 需要对表BASE_MRI_DEVICE的COMPNAME.F ...

  3. 像烟瘾一样的Adobe Flash,真的戒不掉吗?

    近来对Adobe Flash来说真是段难过的日子.Hacking Team公司外泄的440GB电子邮件数据已成为黑客挖掘安全漏洞的宝藏. 光是Flash就被发现了三个不同的漏洞: l  CVE-201 ...

  4. 在浏览器中使用JS打开并展示PDF文件

    使用jquery.media.js插件 示例: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=& ...

  5. 使用Elasticsearch 与 NEST 库 构建 .NET 企业级搜索

    使用Elasticsearch 与 NEST 库 构建 .NET 企业级搜索 2015-03-26 dotNET跨平台 最近几年出现的云计算为组织和用户带来了福音.组织对客户的了解达到前所未有的透彻, ...

  6. 用sed替换文件中的空格

    请教sed 替换问题 请教各位如何替换多个空格为一个字符,如一个文件中间隔符有是一个空格,有的地方是多个空格,想全部用“|”替换,如何处理,请指教 请教sed 替换问题 [code]sed '/ \+ ...

  7. kubectl命令使用

    语法:   kubectl  [command]  [TYPE] [NAME]  [flags]   1 command:子命令,用于操作Kubernetes集群资源对象的命令,如create, de ...

  8. servlet 转发和超链接转发

    超链接属于客户端跳转,request是无法取得属性的 我们知道一个jsp相当与一个servlet 例如,客户端请求A.jsp页面,在A.jsp页面调用request.getAttribute方法放入属 ...

  9. 【LeetCode】22. Generate Parentheses (2 solutions)

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

  10. PLSQL常用配置之窗口/版面保存、SQL格式化/美化、SQL注释\去掉注释等快捷键配置、登陆历史修改配置

    http://blog.csdn.net/hyeidolon/article/details/8251791   PLSQL常用配置之窗口/版面保存.SQL格式化/美化.SQL注释\去掉注释等快捷键配 ...