Skip to content

Instantly share code, notes, and snippets.

@1682105
Created May 22, 2025 11:13
Show Gist options
  • Save 1682105/8e5febac91a4ab2fd1ed7ed0a4cc7897 to your computer and use it in GitHub Desktop.
Save 1682105/8e5febac91a4ab2fd1ed7ed0a4cc7897 to your computer and use it in GitHub Desktop.
字符串练习
sentence = "The quick brown fox jumps over the lazy dog."
# 将字符串按空格分割成列表
words = sentence.split()
print("1. 分割字符串:", words)
# 从words中提取第4个单词,并转换成大写
fourth_word_upper = words[3].upper()
print("2. 第4个单词大写形式:", fourth_word_upper)
# 将原字符串中的单词"lazy"替换成"active"
replace_sentence = sentence.replace("lazy", "active")
print("3. 替换'lazy'为'active':", replace_sentence)
# 统计处理后的字符串中字母'o'的出现次数
count = replace_sentence.count("o")
print("4. 字符'o'出现的次数:", count)
# 检查最终的字符串是否包含子字符串"quick brown"
contains_substring = "quick brown" in replace_sentence
print("5. 是否包含子字符串'quick brown':", contains_substring)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment