python3字符串属性(二)

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 !  '

 

posted @ 2016-03-11 17:11  hb91  阅读(537)  评论(0编辑  收藏  举报