日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python3字符串属性(二)

發布時間:2023/12/18 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 ! '

?

轉載于:https://www.cnblogs.com/hb91/p/5266456.html

總結

以上是生活随笔為你收集整理的python3字符串属性(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。