Python字串 – 在本文中,我將向你展示在Python程式設計中, python 查詢字串資料普遍都會使用的11方法 (python isalnum, python isdecimal, python isdigit, python isnumeric, python isalpha, python islower, python isupper, python isspace, python count, python max, python min, python lower, python capitalize)
方法 | 例子: |
搜查的字串是否全是數字: string.isalnum() | str1 = “I am the king of the world”; print(str1.isalnum()); # False str2 = “Sorry 1999”; print(str2.isalnum()); # False str3 = “30624700”; print(str3.isalnum()); # True |
string 只包含数字: string.isdecimal() | str1 = “I am the king of the world”; print(str1.isdecimal()); # False str2 = “Sorry 1999”; print(str2.isdecimal()); # False str3 = “30624700”; print(str3.isdecimal()); # True |
string 只包含数字: string.isdigit() | str1 = “I am the king of the world”; print(str1.isdigit()); # False str2 = “Sorry 1999”; print(str2.isdigit()); # False str3 = “30624700”; print(str3.isdigit()); # True |
string 只包含数字: string.isnumeric() | str1 = “I am the king of the world”; print(str1.isnumeric()); # False str2 = “Sorry 1999”; print(str2.isnumeric()); # False str3 = “30624700”; print(str3.isnumeric()); # True |
string 只包含字母: string.isalpha() | str1 = “I am the king of the world”; print(str1.isalpha()); # False str2 = “Sorry 1999”; print(str2.isalpha()); # False str3 = “30624700”; print(str3.isalpha()); # False str4 = “jprogramstudy”; print(str4.isalpha()); # True |
string 只包含細楷英文字: string.islower() | str1 = “I am the king of the world”; print(str1.islower()); # False str2 = “Sorry 1999”; print(str2.islower()); # False str3 = “30624700”; print(str3.islower()); # False str4 = “sleeping”; print(str4.islower()); # True |
string 只包含大楷英文字: string.isupper() | str1 = “I am the king of the world”; print(str1.isupper()); # False str2 = “Sorry 1999”; print(str2.isupper()); # False str3 = “30624700”; print(str3.isupper()); # False str4 = “KING”; print(str4.isupper()); # True |
string 只包含空格: string.isspace() | str1 = “I am the king of the world”; print(str1.isspace()); # False str2 = “Sorry 1999”; print(str2.isspace()); # False str3 = “30624700”; print(str3.isspace()); # False str4 = ” “; print(str4.isspace()); # True |
string包含多少個要搜尋的關鍵字: string.count(str) | str1 = “I am the king of the world”; print(str1.count(“king”)); # 1 str2 = “Sorry 1999”; print(str2.count(“king”)); # 0 str3 = “30624700”; print(str3.count(“king”)); # 0 str4 = ” king king king “; print(str4.count(“king”)); # 3 |
string中最大的字 max(str) | str1 = “I am the king of the world”; print(max(str1)); # w str2 = “Sorry 1999”; print(max(str2)); # y str3 = “30624700”; print(max(str3)); # 7 |
string中最小的字: min(str) | str1 = “I am the king of the world”; print(min(str1)); # str2 = “Sorry 1999”; print(min(str2)); # str3 = “30624700”; print(min(str3)); # 0 |
相關頁面:
Python字串 – 字串String功能速查表 – 尋找,擷取,對比,換成大小寫