MST

星途 面试题库

面试题:Python字符串中等难度操作题

给定一个字符串s = 'hello world python', 使用Python字符串操作方法实现以下功能:1. 提取出子串'world'。2. 将字符串中的'python'替换为'java' 。3. 计算字符串中字母'l'出现的次数。请写出实现代码。
22.0万 热度难度
编程语言Python

知识考点

AI 面试

面试题答案

一键面试
s = 'hello world python'
# 1. 提取出子串'world'
sub_str = s[s.index('world'):s.index('world')+len('world')]
# 2. 将字符串中的'python'替换为'java'
new_s = s.replace('python', 'java')
# 3. 计算字符串中字母'l'出现的次数
count = s.count('l')

print(sub_str)
print(new_s)
print(count)