总结

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. EXCEPTION-SQL语句

      CreateTime--2017年1月12日14:37:52Author:Marydon 声明:异常类文章主要是记录了我遇到的异常信息及解决方案,解决方案大部分都是百度解决的,(这里只是针对我遇到 ...

  2. uva 699 The Falling Leaves(建二叉树同一时候求和)

    本来看着挺难的.大概是由于我多瞟了一眼题解,瞬间认为简单多了.做题就得这样,多自己想想.如今是 多校联赛,然而我并不会做. .. .慢慢来,一直在努力. 分析: 题上说了做多不会超过80行.所以能够开 ...

  3. 【SPSS】软件介绍

    SPSS软件是美国斯坦福大学三位学生1968年研制开发的统计软件,SPSS是Statistical Package for Social Science(社会科学软件统计包)的缩写,2000年SPSS ...

  4. Linear Algebra Courses

    Lecture 1:Demonstrate the columns of a matrix (imagine the vectors) in N-dimension space.How to mult ...

  5. iOS自定义从底部弹上来的View

    概述 自定义蒙层弹起View,点击一下遮罩或界面上关闭按钮,页面会自动下去(从上向下) 详细 代码下载:http://www.demodashi.com/demo/10724.html 在一些少数据没 ...

  6. 实现ScrollviewSupportMaxHeight

    public class ScrollviewSupportMaxHeight extends ScrollView {         public final int MAX_HEIGHT = 1 ...

  7. 【laravel5.4】引入自定义类库+卸载已有的自定义库(以引入钉钉应用为例)composer dumpautoload -o

    本文之前,首先感谢: Azeroth_Yang  传送门:https://blog.csdn.net/zwrj1130/article/details/73467320 强烈建议引入的类 都是含有命名 ...

  8. MongoDB-开始学习MongoDB(一)

    先来看看MongoDB的优缺点: 优点:简单的扩展.快速的读写.灵活的数据类型 缺点:不支持对SQL的支持.支持的特性不够丰富.现有产品不够成熟 应用场景: 适用场景: 持久化缓存层.实时的高效性(读 ...

  9. 【LeetCode】51. N-Queens

    N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no tw ...

  10. 推荐6款极具个性化的在线生成logo的网站

    http://www.douban.com/group/topic/10434724/