1. Title Description
Please implement a function, a string to replace each space to "20%." For example, when the string is We Are Happy. After the string is replaced after We% 20Are% 20Happy.
2. Replace violence
First create a null string, then for each character;
if the character is not a space, the character is added to the new end of the string;
if the character is a space, add '% 20' to the new end of the string.
class Solution:
# s 源字符串
def replaceSpace(self, s):
# write code here
new_s = ''
for i in s:
if i == ' ':
i = '%20'
new_s = new_s + i
return new_s
3. python built-in functions
class Solution:
# s 源字符串
def replaceSpace(self, s):
# write code here
return s.replace(" ","%20")