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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/458208.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/458208.shtml
英文地址,請注明出處:http://en.pswp.cn/news/458208.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

使用libsvm中的svm_cross_validation函數進行交叉驗證

from:https://blog.csdn.net/tao1107291820/article/details/51581322 在libsvm的使用中&#xff0c;為了得到更好的c、gama參數&#xff0c;可以通過多次使用libsvm中的svm_cross_validation函數進行參數尋優&#xff0c;下面是svm_cross_validation的一種使用方法&#xff1…

JavaScript中eval()函數

eval調用時&#xff0c;實例為eval( "( javascript代碼 )" )&#xff0c; eval() 函數可將字符串轉換為代碼執行&#xff0c;并返回一個或多個值。轉載于:https://www.cnblogs.com/lxcmyf/p/5703640.html

輕松談話:談話的力量

如何與她人搭話&#xff1f;&#xff1f; 第一&#xff1a;給別人一個好印象 1、環境&#xff1a;通過共同環境來激發興趣&#xff0c;比如&#xff1a;在球場&#xff0c;你覺得誰會贏。 2、對方:多數人喜歡談論自己。 3、自己&#xff1a;主動表明意圖&#xff0c;要真誠。 第…

函數的二義性與函數對象的傳遞問題(通過實現vector的to_string示例)

許多時候&#xff0c;我們想要直接打印容器的內容&#xff0c;比如 std::vector<int> a { 1, 2, 3 }; 可以打印出[1, 2, 3]。 參考標準庫&#xff0c;可以寫出一個帶有迭代器的to_string函數&#xff1a; template <typename Iter, typename Func> std::string to…

libSVM介紹(二)

from&#xff1a;https://blog.csdn.net/carson2005/article/details/6539192 鑒于libSVM中的readme文件有點長&#xff0c;而且&#xff0c;都是采用英文書寫&#xff0c;這里&#xff0c;我把其中重要的內容提煉出來&#xff0c;并給出相應的例子來說明其用法&#xff0c;大家…

四則運算題2

本題新學知識點&#xff1a; itoa函數 char *itoa( int value, char *string,int radix);[1]原型說明&#xff1a;value&#xff1a;欲轉換的數據。string&#xff1a;目標字符串的地址。radix&#xff1a;轉換后的進制數&#xff0c;可以是10進制、16進制等。程序實例:#includ…

c++調用Libsvm

libSVM中的readme中文版&#xff1a;http://blog.csdn.net/carson2005/article/details/6539192 LibSVM的package中的Readme文件中介紹了怎樣具體的使用LibSvm&#xff0c;可以在Dos下以命令形式進行調用&#xff0c;也可以用程序包中提供的GUI程序Svm-toy進行圖形化的操作。sv…

STL -set

轉載自&#xff1a;http://blog.csdn.net/LYHVOYAGE/article/details/22989659 set集合容器實現了紅黑樹&#xff08;Red-Black Tree&#xff09;的平衡二叉檢索樹的的數據結構&#xff0c; 在插入元素時&#xff0c;它會自動調整二叉樹的排列&#xff0c;把該元素放到適當的位…

【機器學習實戰之一】:C++實現K-近鄰算法KNN

本文不對KNN算法做過多的理論上的解釋&#xff0c;主要是針對問題&#xff0c;進行算法的設計和代碼的注解。 KNN算法&#xff1a; 優點&#xff1a;精度高、對異常值不敏感、無數據輸入假定。 缺點&#xff1a;計算復雜度高、空間復雜度高。 適用數據范圍&#xff1a;數值…

libsvm C++ 代碼參數說明匯總

幾個重要的數據結構 2.1 struct svm_problem {int l; // 記錄樣本的總數double *y; // 樣本所屬的標簽(1, -1)struct svm_node **x; // 指向樣本數據的二維數組(即一個矩陣&#xff0c;行數是樣本數&#xff0c;列數是特征向量維度) }; 2.2 struct svm_node {int …

javascript設計模式-繼承

javascript繼承分為兩種&#xff1a;類式繼承&#xff08;原型鏈、extend函數&#xff09;、原型式繼承&#xff08;對繼承而來的成員的讀和寫的不對等性、clone函數&#xff09;。 類式繼承-->prototype繼承&#xff1a; 1 function Person(name){2 this.name …

GIS基礎軟件及操作(二)

原文 GIS基礎軟件及操作(二) 練習二、管理地理空間數據庫 1.利用ArcCatalog 管理地理空間數據庫 2.在ArcMap中編輯屬性數據 第1步 啟動 ArcCatalog 打開一個地理數據庫 當 ArcCatalog打開后&#xff0c;點擊, 按鈕&#xff08;連接到文件夾&#xff09;. 建立到包含練習數據的…

libSVM分類小例C++

from&#xff1a;http://www.doczj.com/list_31/ 使用libSVM求解分類問題的C小例 1.libSVM簡介 訓練模型的結構體 struct svm_problem//儲存參加計算的所有樣本 { int l; //記錄樣本總數 double *y; //指向樣本類別的組數 //prob.y new double[prob.l]; struct svm_node …

qunit 前端腳本測試用例

首先引用qunit 測試框架文件 <link rel"stylesheet" href"qunit-1.22.0.css"> <script src"qunit-1.22.0.js"></script> <div id"qunit"></div> <div id"qunit-fixture"></div>…

非常規文件名刪除

生活中我們偶爾會遇到這樣一件事&#xff1a;走在路上&#xff0c;突然感覺鞋底有東西&#xff0c;抬腳一看&#xff0c;是個泡泡糖。拿不掉&#xff0c;走路還一粘一粘的。要多難受有多難受&#xff01;同樣在linux中也有這么一種文件名。看著不舒服&#xff0c;卻刪不掉。今天…

Machine Learning(Stanford)| 斯坦福大學機(吳恩達)器學習筆記【匯總】

from&#xff1a;https://blog.csdn.net/m399498400/article/details/52556168 定義本課程常用符號 訓練數據&#xff1a;機器用來學習的數據 測試數據&#xff1a;用來考察機器學習效果的數據&#xff0c;相當于考試。 m 訓練樣本的數量&#xff08;訓練集的個數) x 輸入的…

PHP OOP

類跟對象的關系類是對象的抽象(對象的描述(屬性)&#xff0c;對象的行為(方法))對象是類的實體面相對象的三大特征&#xff1a;封裝、集成、多態自定義類Class Person{}屬性定義屬性是類里面的成員&#xff0c;所以要定義屬性的前提條件是需要聲明一個類Class Person{public $n…

kv存儲對抗關系型數據庫

http://www.searchdatabase.com.cn/showcontent_52657.htm轉載于:https://www.cnblogs.com/hexie/p/5276034.html

模板匹配算法

from&#xff1a;https://blog.csdn.net/zhi_neng_zhi_fu/article/details/51029864 模板匹配(Template Matching)算法 模板匹配&#xff08;Template Matching&#xff09;是圖像識別中最具代表性的方法之一。它從待識別圖像中提取若干特征向量與模板對應的特征向量進行比較…

關于linux用戶權限的理解

創建用戶useradd 用戶名創建用戶組groupadd 組名查看用戶Idid 用戶修改文件權限chmod 777 文件名或目錄-R 遞歸修改用戶數組chown 屬主&#xff1a;屬組 文件名或目錄名-R 遞歸轉載于:https://blog.51cto.com/1979431/1833512