fork download
  1. #原始列表
  2. nums=[5,12,7,9,21,7,14]
  3. #统计数字7出现的次数
  4. count_seven=nums.count(7)
  5. print("数字7出现的次数:",count_seven)
  6. #判断99是否存在, 不存在则添加
  7. if 99 not in nums:
  8. nums.append(99)
  9. print("添加99后的列表:",nums)
  10. #删除第一次出现的数字7
  11. nums.remove(7) #只删除第一个7
  12. print("删除第一个7后的列表:",nums)
  13. #取出第2个到第4个元素(索引1到3)
  14. sub_list=nums[1:4]
  15. print("第2~4个元素组成的列表:",sub_list)
  16. msg = "hello 123 WORLD"
  17. #统计长度和小写字母个数
  18. length = len(msg)
  19. lower_count = sum(1 for ch in msg if ch. islower())
  20. print("长度:",length)
  21. print("小写字母个数:",lower_count)
  22. #替换并转小写
  23. new_msg = msg.replace("WORLD","Python")
  24. result = new_msg.lower()
  25. print("结果:",result)
Success #stdin #stdout 0.04s 9372KB
stdin
Standard input is empty
stdout
数字7出现的次数: 2
添加99后的列表: [5, 12, 7, 9, 21, 7, 14, 99]
删除第一个7后的列表: [5, 12, 9, 21, 7, 14, 99]
第2~4个元素组成的列表: [12, 9, 21]
长度: 15
小写字母个数: 5
结果: hello 123 python