1、S.isdecimal() -> bool
    Return True if there are only decimal characters in S, False otherwise. 字符串如果是十进制,返回True。

2、S.isdigit() -> bool
     Return True if all characters in S are digits and there is at least one character in S, False otherwise.
3、S.isnumeric() -> bool
    Return True if there are only numeric characters in S,
    False otherwise.

数字

1 >>> num='1'
2 >>> num.isdigit()
3 True
4 >>> num.isdecimal()
5 True
6 >>> num.isnumeric()
7 True

汉字

1 >>> num="二十四"
2 >>> num.isdigit()
3 False
4 >>> num.isdecimal()
5 False
6 >>> num.isnumeric()
7 True

字节(和字符串很像,但在python中不是同一类型)

 1 >>> num=b'1'
 2 >>> num.isdigit()
 3 True
 4 >>> num.isdecimal()
 5 Traceback (most recent call last):
 6   File "<stdin>", line 1, in <module>
 7 AttributeError: 'bytes' object has no attribute 'isdecimal'
 8 >>> num.isnumeric()
 9 Traceback (most recent call last):
10   File "<stdin>", line 1, in <module>
11 AttributeError: 'bytes' object has no attribute 'isnumeric'

1 >>> a=b'abc'
2 >>> type(a)
3 <class 'bytes'>
4 >>> a='abc'
5 >>> type(a)
6 <class 'str'>

a=b'abc'不是字符串,是字节类型。

"Python的字符串类型是str,在内存中以Unicode表示,一个字符对应若干个字节。如果要在网络上传输,或者保存到磁盘上,就需要把str变为以字节为单位的bytes。"

(http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431664106267f12e9bef7ee14cf6a8776a479bdec9b9000)

4、S.islower() -> bool
    Return True if all cased characters in S are lowercase and there is at least one cased character in S, False otherwise.

字符串里的至少有一个字母且所有的字母为小写

 1 >>> a='abc'
 2 >>> a.islower()
 3 True
 4 >>> a='abcD'
 5 >>> a.islower()
 6 False
 7 >>> a='abc1'
 8 >>> a.islower()
 9 True
10 >>> a='abc1-'
11 >>> a.islower()
12 True
13 >>> a='1-'
14 >>> a.islower()
15 False

5、S.isupper() -> bool
    Return True if all cased characters in S are uppercase and there is
    at least one cased character in S, False otherwise.

用法参见islower()

6、 S.isprintable() -> bool
    Return True if all characters in S are considered printable in repr() or S is empty, False otherwise.

7、S.isspace() -> bool
    
    Return True if all characters in S are whitespace
    and there is at least one character in S, False otherwise.

字符串至少一个字符,且所有字符都是空格。

1 >>> a='abc  '
2 >>> a.isspace()
3 False
4 >>> a[3:].isspace()
5 True

8、  S.istitle() -> bool
    
    Return True if S is a titlecased string and there is at least one
    character in S, i.e. upper- and titlecase characters may only
    follow uncased characters and lowercase characters only cased ones.
    Return False otherwise.

检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写

1 >>> a='Hello World !'
2 >>> a.istitle()
3 True
4 >>> a='Hello World ,huhu!'
5 >>> a.istitle()
6 False

9、S.join(iterable) -> str
    
    Return a string which is the concatenation of the strings in the
    iterable.  The separator between elements is S.    连接字符.join(可以迭代的字符串)

1 >>> a='Hello World ,huhu!'
2 >>> '-'.join(a)
3 'H-e-l-l-o- -W-o-r-l-d- -,-h-u-h-u-!'

1 >>> a=['hello','world','!']
2 >>> b='-'
3 >>> b.join(a)
4 'hello-world-!'

10、S.ljust(width[, fillchar]) -> str            左对齐
    
    Return S left-justified in a Unicode string of length width. Padding is
    done using the specified fill character (default is a space).
方法返回一个原字符串左对齐,并使用空格或其他字符填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。

1 >>> a='abc'
2 >>> a.ljust(6)
3 'abc   '
4 >>> a.ljust(6,'!')
5 'abc!!!'
6 >>> a.ljust(2)
7 'abc'

11、S.rjust(width[, fillchar]) -> str           右对齐
    
    Return S right-justified in a string of length width. Padding is
    done using the specified fill character (default is a space).

12、S.lower() -> str
    
    Return a copy of the string S converted to lowercase.

13、S.upper() -> str
    
    Return a copy of S converted to uppercase.

1 >>> a='Hello World !'
2 >>> a.lower()
3 'hello world !'
4 >>> a.upper()
5 'HELLO WORLD !'
6 >>> a
7 'Hello World !'

14、 S.strip([chars]) -> str    移除头部和尾部字符
    
    Return a copy of the string S with leading and trailing
    whitespace removed.
    If chars is given and not None, remove characters in chars instead.

  S.lstrip([chars]) -> str    移除头部字符
    
    Return a copy of the string S with leading whitespace removed.
    If chars is given and not None, remove characters in chars instead.

  S.rstrip([chars]) -> str    移除尾部字符
    
    Return a copy of the string S with trailing whitespace removed.
    If chars is given and not None, remove characters in chars instead.

1 >>> a='  hello world !  '
2 >>> a.strip()
3 'hello world !'
4 >>> a.lstrip()
5 'hello world !  '
6 >>> a.rstrip()
7 '  hello world !'
8 >>> a
9 '  hello world !  '

转载于:https://www.cnblogs.com/hb91/p/5266456.html

python3字符串属性(二)相关推荐

  1. 20141230 mysql数值类型和列属性二

    20141230 mysql数值类型和列属性二 枚举字符串 枚举字符串指的是在定义之初就确定要存放的字符串有哪些,然后在数据进行存储的时候就只能存储已经定义过的字符串,只能使用任意的一个字符串.(单选 ...

  2. 微信小程序业务-字符串生成二维码(weapp-qrcode)

    微信小程序业务-字符串生成二维码(weapp-qrcode) 前言 邂逅weapp-qrcode 基本使用 详细参数 小程序组件中使用 image属性详解 想使用网络图片? 参考地址 前言 在小程序项 ...

  3. Js 字符串属性及方法

    Js 字符串属性及方法 本文记录下字符串相关属性及方法,参考文档 MDM String. 一.语法: 'hello world' "hello world" "中文&qu ...

  4. python字节流转化为字符串报错_python3.x,_关于Python3字符串转换为字节流的问题,python3.x - phpStudy...

    关于Python3字符串转换为字节流的问题 un = 'BZh91AY&SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah ...

  5. Python3 字符串

    Python3 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.例如: var1 = 'Hello ...

  6. python3 字符串 转 ascii码

    在这里先解释一下ASCII,Unicode 和 UTF-8这三者的关系 最早出现的是ASCII,一共包含128个字符(一个字节可以表示256种状态,但第一位所有默认为0,所以只有128种).在接下来计 ...

  7. python画二维散点图-python3实现绘制二维点图

    如下所示: import matplotlib.pyplot as plt plt.plot([1,2,3],[4,5,6],'ro') plt.show()#这个智障的编辑器,,,看来高版本的确修复 ...

  8. Python基础数据类型之字符串(二)

    Python基础数据类型之字符串(二) 一.字符串的常规操作 二.字符串的大小写转换 1.首字母大写 2. 每个单词首字母大写 3.大写转化为小写 4.所有字母变成大写字母 二.验证码忽略大小写 三. ...

  9. JavaScript 常用内置对象(字符串属性、Math对象、Array数组对象)

    1.字符串属性 <script> var test_var = "I Iove you"; console.log(test_var.charAt(3)) //char ...

最新文章

  1. rust怎么放篝火_如何为你的露营活动搭建一个持久温暖的篝火
  2. Python学习小结---粗略列表解析
  3. 去掉easyui datagrid内部虚线的方式。
  4. 防止ASP.NET按钮多次提交的办法
  5. linux的常用操作——gcc
  6. XSS学习笔记(一)
  7. R-CNN学习笔记5:Faster R-CNN
  8. 【离散数学】集合论 第四章 函数与集合(5) 集合的基数、可数与不可数集合
  9. Cadence软件包集成了那么多软件,傻傻分不清?
  10. 统计学基础知识(一)
  11. 以色列Aladdin HASP SRM(AES-128)加密狗破解经验分享
  12. LeetCode反转链表图解
  13. 中不中奖,都是抽奖程序的锅?
  14. 推荐3个计算机专业的英文电子书下载网站
  15. QQ空间迁移_【小米摄像头跨局域网NAS存储】
  16. android-更新UI的几种方式
  17. 在线音乐播放项目——BY音乐
  18. 北京大学MOOC 程序设计与算法(三)魔兽世界三(开战)
  19. blinker点灯开关组件
  20. Linux 搭建Owncloud 私有云

热门文章

  1. Apollo进阶课程⑲丨Apollo感知之旅——感知算法
  2. 机器学习笔记(十六):大规模机器学习
  3. Java基础部分快速复习(以前复习的时候放在自己的新浪博客上)
  4. java获取xlsx某列数据_Java读取Excel指定列的数据详细教程和注意事项
  5. 安卓用于组件传递参数的对象是_入门篇:7.组件2:Android Service-service的数据传递与通信...
  6. joptionpane java_Java JOptionPane
  7. mysql 性能问题_mysql性能问题
  8. 计算机专业的三行情书,各专业三行情书,看懂你就是全能学霸!
  9. dockerfile 安装mysql_dockerfile构建mysql镜像
  10. CentOS下安装Memcached,Linux下安装Memcached,centos下安装memcached,linux下安装memcached...