python3----转换大小写(upper lower capitalize and title)

和其他语言一样,Python为string对象提供了转换大小写的方法:upper() 和 lower()。还不止这些,Python还为我们提供了首字母大写,其余小写的capitalize()方法,以及所有单词首字母大写,其余小写的title()方法。函数较简单,看下面的例子:

 1 s = ‘hEllo pYthon‘
 2 print(s.upper())
 3 print(s.lower())
 4 print(s.capitalize())
 5 print(s.title())
 6
 7 results:
 8
 9 HELLO PYTHON
10 hello python
11 Hello python
12 Hello Python

判断大小写

Python提供了isupper(),islower(),istitle()方法用来判断字符串的大小写。注意的是:
1. 没有提供 iscapitalize()方法,下面我们会自己实现,至于为什么Python没有为我们实现,就不得而知了。
2. 如果对空字符串使用isupper(),islower(),istitle(),返回的结果都为False。

1 print(‘A‘.isupper()) #True
2 print(‘A‘.islower()) #False
3 print(‘Python Is So Good‘.istitle()) #True
4 #print ‘Dont do that!‘.iscapitalize() #错误,不存在iscapitalize()方法

原文地址:https://www.cnblogs.com/jonm/p/8270324.html

时间: 2024-07-31 22:02:04

python3----转换大小写(upper lower capitalize and title)的相关文章

[非凡程序员]UIKit 手写控件转换大小写

// //  ViewController.m //  手写转换大小写 // //  Created by 非凡程序员 on 15/11/11. //  Copyright (c) 2015年 Querida. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [

map reduce 用法 str处理lower() capitalize()

-- 1 s='123456' 2 l={'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}[s[0]] 3 print(l) 取出dic里面key的元素 1 def normalize(name): 2 tempn=name.lower().capitalize() 3 return tempn 4 L1 = ['adam', 'LISA', 'barT'] 5 L2 = list(map(normalize, L1)) 6

JS正则表达式转换大小写

JS应用正则表达式转换大小写,以首字母大写,其它字母小写为例. Js中应用正则表达式转换大小写. 以下首字母大写,其它字母小写 <script type="text/javascript"> function replaceReg(reg,str){ str = str.toLowerCase(); return str.replace(reg,function(m){return m.toUpperCase()}) } var reg = /\b(\w)|\s(\w)/g

JS中应用正则表达式转换大小写

JS中应用正则表达式转换大小写,代码很简单,看代码: 以下首字母大写,其它字母小写 [javascript] view plaincopy <script type="text/javascript"> function replaceReg(reg,str){ str = str.toLowerCase(); return str.replace(reg,function(m){return m.toUpperCase()}) } var reg = /\b(\w)|\s

Python中capitalize()与title()的区别

capitalize()与title()都可以实现字符串首字母大写.主要区别在于:capitalize(): 字符串第一个字母大写title(): 字符串内的所有单词的首字母大写 例如: >>> str='huang bi quan' >>> str.capitalize() 'Huang bi quan' #第一个字母大写 >>> str.title() 'Huang Bi Quan' #所有单词的首字母大写 非字母开头的情况: >>&g

python中的upper、lower、capitalize、title

upper()字符串中字母由小写变为大写 lower()字符串中字母由大写变为小写 capitalize()字符串中字母首字母大写其余小写 title()字符串中字母每个单词的首字母大写其余小写 举个列子: 1 a = "hello" 2 b = "WORLD" 3 c = "hello" 4 d = "hello world" 5 a1 = a.upper() 6 b1 = b.lower() 7 c1 = c.capita

python字符串相关函数 *title *upper *lower *swapcase *len *count *find *index *starts with *endswith *isalpha *isdecimal *split *center *strip *replace

# ### 字符串相关函数 (函数就是方法的意思,完成某个功能)""" 语法: 字符串.函数 """ #*capitalize 字符串首字母大写 strvar = "this is my world"res = strvar.capitalize()print(res) # *title 每个单词的首字母大写 (非字母类的就可以让后面字符大写)# strvar = "this is my world"str

python3 判断大小写

转自http://wangwei007.blog.51cto.com/68019/1134323 # 一.pyhton字符串的大小写转换, 常用的有以下几种方法: # 1.对字符串中所有字符(仅对字母有效)的大小写转换, 有两个方法: print('just to test it'.upper()) # 所有字母都转换成大写 print('JUST TO TEST IT'.lower()) # 所有字母都转换成小写 # 2.对字符串中的字符(仅对字母有效)部分大小写转换: print('JUST

python2 到 python3 转换工具 2to3

windows系统下的使用方法: (1)将python安装包下的Tools/Scripts下面的2to3.py拷贝到需要转换文件目录中. (2)dos切换到需要转换的文件目录下,运行命令2to3.py test.py 可打印test.py,在python2与python3的差异. (3)dos切换到需要转换的文件目录下,运行命令2to3.py -w test.py 将test.py备份为test.py.bak文件 test.py将相应的格式及相应包改写为python3 恭喜你,python2到p