码迷,mamicode.com
首页 > 编程语言 > 详细

python 字符串相关处理方法

时间:2019-01-31 22:59:51      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:大写   pytho   字节   浮点   val   object   数字   float   ace   

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
View Code

 

python 字符串相关处理方法

标签:大写   pytho   字节   浮点   val   object   数字   float   ace   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!