6.9 字符串的课后练习讲解

练习

给定字符串:Lonely North Snow

  1. 统计字符串中有多少个n

  2. 将空格替换为|

  3. 按照|进行分割得到列表

str = "Lonely North Snow"
print(str)

count = str.count("n")
print(count)

str1 = str.replace(" ", "|")
print(str1)

str2 = str1.split("|")
print(str2)

输出结果:

Lonely North Snow 2 Lonely|North|Snow ['Lonely', 'North', 'Snow']

Last updated

Was this helpful?