fork download
  1.  
  2. # ========== 列表操作 ==========
  3. nums = [5, 12, 7, 9, 21, 7, 14]
  4. count_seven = nums.count(7)
  5. print("数字7出现的次数:", count_seven)
  6. if 99 not in nums:
  7. nums.append(99)
  8. print("添加99后的列表:", nums)
  9.  
  10. nums.remove(7)
  11. print("删除第一个7后的列表:", nums)
  12. sub_list = nums[1:4]
  13. print("第2~4个元素组成的列表:", sub_list)
  14.  
  15. # ========== 字符串操作 ==========
  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)
  26.  
Success #stdin #stdout 0.07s 14176KB
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