python 字符串相关处理方法

s为字符串
s.isalnum() 所有字符都是数字或者字母
s.isalpha() 所有字符都是字母
s.isdigit() 所有字符都是数字
s.islower() 所有字符都是小写
s.isupper() 所有字符都是大写
s.istitle() 所有单词都是首字母大写,像标题
s.isspace() 所有字符都是空白字符、\t、\n、\r

 1 判断是整数还是浮点数
 2 a=123
 3 b=123.123
 4
 5 >>>isinstance(a,int)
 6 True
 7 >>>isinstance(b,float)
 8 True
 9 >>>isinstance(b,int)
10 False

 1 python中str函数isdigit、isdecimal、isnumeric的区别
 2 num = "1" #unicode
 3 num.isdigit() # True
 4 num.isdecimal() # True
 5 num.isnumeric() # True
 6
 7 num = "1" # 全角
 8 num.isdigit() # True
 9 num.isdecimal() # True
10 num.isnumeric() # True
11
12 num = b"1" # byte
13 num.isdigit() # True
14 num.isdecimal() # AttributeError ‘bytes‘ object has no attribute ‘isdecimal‘
15 num.isnumeric() # AttributeError ‘bytes‘ object has no attribute ‘isnumeric‘
16
17 num = "IV" # 罗马数字
18 num.isdigit() # True
19 num.isdecimal() # False
20 num.isnumeric() # True
21
22 num = "四" # 汉字
23 num.isdigit() # False
24 num.isdecimal() # False
25 num.isnumeric() # True
26
27 ===================
28 isdigit()
29 True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字
30 False: 汉字数字
31 Error: 无
32
33 isdecimal()
34 True: Unicode数字,,全角数字(双字节)
35 False: 罗马数字,汉字数字
36 Error: byte数字(单字节)
37
38 isnumeric()
39 True: Unicode数字,全角数字(双字节),罗马数字,汉字数字
40 False: 无
41 Error: byte数字(单字节)
42
43 ================
44 import unicodedata
45
46 unicodedata.digit("2") # 2
47 unicodedata.decimal("2") # 2
48 unicodedata.numeric("2") # 2.0
49
50 unicodedata.digit("2") # 2
51 unicodedata.decimal("2") # 2
52 unicodedata.numeric("2") # 2.0
53
54 unicodedata.digit(b"3") # TypeError: must be str, not bytes
55 unicodedata.decimal(b"3") # TypeError: must be str, not bytes
56 unicodedata.numeric(b"3") # TypeError: must be str, not bytes
57
58 unicodedata.digit("Ⅷ") # ValueError: not a digit
59 unicodedata.decimal("Ⅷ") # ValueError: not a decimal
60 unicodedata.numeric("Ⅷ") # 8.0
61
62 unicodedata.digit("四") # ValueError: not a digit
63 unicodedata.decimal("四") # ValueError: not a decimal
64 unicodedata.numeric("四") # 4.0

原文地址:https://www.cnblogs.com/laoyw/p/10344093.html

时间: 2024-08-02 03:37:59

python 字符串相关处理方法的相关文章

python 字符串内置方法整理

编码相关内置方法: (1)    str.encode(encoding='utf-8'):返回字符串编码,encoding指定编码方式. >>> a = 'I love Python' >>> a.encode(encoding='utf-8') b'I love Python' >>> a.encode(encoding='gbk') b'I love Python' >>> b.encode(encoding='utf-8')

python字符串操作实方法大合集

python字符串操作实方法大合集,包括了几乎所有常用的python字符串操作,如字符串的替换.删除.截取.复制.连接.比较.查找.分割等,需要的朋友可以参考下: #1.去空格及特殊符号 s.strip().lstrip().rstrip(',') #2.复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sStr1 = 'strcpy2' print sStr2 #3.连接字符串 #strcat(sStr1,sStr2) sStr

7.python字符串-内置方法分析

上篇对python中的字符串进行了列举和简单说明,但这些方法太多,逐一背下效率实在太低,下面我来对这些方法安装其功能进行总结: 1.字母大小写相关(中文无效) 1.1 S.upper() -> string 返回一个字母全部大写的副本 1.2 S.lower() -> string 返回一个字母全是小写的副本 1.3 S.swapcase() -> string 返回一个字母大小写转换后的副本 1.4 S.title() -> string 将单词的首字母大写,即为所谓的标题 方框

python字符串-内置方法用法分析

1.字母大小写相关(中文无效) 1.1 S.upper() -> string 返回一个字母全部大写的副本 1.2 S.lower() -> string 返回一个字母全是小写的副本 1.3 S.swapcase() -> string 返回一个字母大小写转换后的副本 1.4 S.title() -> string 将单词的首字母大写,即为所谓的标题 方框里是中文的编码,可以发现 s 还是大写了,说明会无视其他类型的字符,找到英文单词就将其首字母大写 1.6 S.capitaliz

Python 字符串分割的方法

在平时工作的时候,发现对于字符串分割的方法用的比较多,下面对分割字符串方法进行总结一下:第一种:split()函数split()函数应该说是分割字符串使用最多的函数用法:str.split('分割符')通过该分割操作后,会返回一个列表. 注:当然如果你的字符串含有一个或者多个空格就直接 str.split() 就可以了 例如: >>> a = "hello,python,Good Night" >>> a.split(',') ['hello', '

python字符串内置方法

网上已经有很多,自己操作一遍,加深印象. dir dir会返回一个内置方法与属性列表,用字符串'a,b,cdefg'测试一下 dir('a,b,cdefg') 得到一个列表 ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__'

python字符串相关的函数

有些是字符串对象的方法,有些是内建库的方法 split分割字符串 find 查找字符串 for c in str:  遍历字符串 len 获取字符串长度 int    将字符串转换成int str   将数字转换成字符串 list.join   将列表连接成字符串 以下是字符串截取的方法以及例子,要注意区间的开和闭 str[1:3]  字符串截取第一位到第三位之间的 str[:]截取字符串全部字符 str[6:]截取字符串从第六个到结尾 str[6]取第6个字符 str[:-3]截取从开头到倒数

python字符串相关操作

字符串搜索相关搜索指定字符串,没有返回-1:str.find('t')指定起始位置搜索:str.find('t',start)指定起始及结束位置搜索:str.find('t',start,end)从右边开始查找:str.rfind('t')搜索到多少个指定字符串:str.count('t')上面所有方法都可用index代替,不同的是使用index查找不到会抛异常,而find返回-1字符串判断相关是否以start开头:str.startswith('start')是否以end结尾:str.ends

Python 字符串中 startswith()方法

Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在指定范围内检查. str.startswith(str, beg=0,end=len(string)); #!/usr/bin/python str = "this is string example....wow!!!"; print str.startswith( 'this' ); print str.star