请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
replace的用法
class Solution:
# s 源字符串
def replaceSpace(self, s):
# write code here
return s.replace(' ','%20')
新建存储空间
class Solution:
# s 源字符串
def replaceSpace(self, s):
# write code here
result = []
for i in range(len(s)):
if s[i] == ' ':
result.append("%")
result.append("2")
result.append("0")
else:
result.append(s[i])
return "".join(result)
字符串替换实战:将空格转为 '%20'
本文介绍如何使用Python实现一个函数,通过replace()方法将字符串中的每个空格替换为%20格式,以满足URL编码需求。通过实例演示了两种不同的解决方案,适合初学者理解基本操作。
3186

被折叠的 条评论
为什么被折叠?



