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中不是同一類型)
?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 False5、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 True8、? 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 False9、S.join(iterable) -> str
?? ?
??? Return a string which is the concatenation of the strings in the
??? iterable.? The separator between elements is S.??? 連接字符.join(可以迭代的字符串)
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).
方法返回一個原字符串左對齊,并使用空格或其他字符填充至指定長度的新字符串。如果指定的長度小于原字符串的長度則返回原字符串。
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.
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.
?
轉載于:https://www.cnblogs.com/hb91/p/5266456.html
總結
以上是生活随笔為你收集整理的python3字符串属性(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小朋友学数据结构(3):二叉树的建立和遍
- 下一篇: ubuntu安装QT4的方法