当前位置:  首页>> 技术小册>> Python合辑3-字符串用法深度总结

str.isalpha()

如果字符串中的所有字符都是字母,该方法返回True;否则返回 False:

  1. # Alphabet string
  2. alphabet_one = "Learning"
  3. print(alphabet_one.isalpha())
  4. # Contains whitspace
  5. alphabet_two = "Learning Python"
  6. print(alphabet_two.isalpha())
  7. # Contains comma symbols
  8. alphabet_three = "Learning,"
  9. print(alphabet_three.isalpha())

Output:

  1. True
  2. False
  3. False

如果字符串字符是字母数字,str.isalnum() 返回 True;如果字符串字符是十进制,str.isdecimal() 返回 True;如果字符串字符是数字,str.isdigit() 返回 True;如果字符串字符是数字,则 str.isnumeric() 返回 True
如果字符串中的所有字符都是小写,str.islower() 返回 True;如果字符串中的所有字符都是大写,str.isupper() 返回 True;如果每个单词的首字母大写,str.istitle() 返回 True:

  1. # islower() example
  2. string_one = "Artificial Neural Network"
  3. print(string_one.islower())
  4. string_two = string_one.lower() # converts string to lowercase
  5. print(string_two.islower())
  6. # isupper() example
  7. string_three = string_one.upper() # converts string to uppercase
  8. print(string_three.isupper())
  9. # istitle() example
  10. print(string_one.istitle())

Output:

  1. False
  2. True
  3. True
  4. True