1. 替换空格
算法:请实现一个函数,把字符串 s 中的每个空格替换成"#30"。
方法一:
import re
def replace_space(s):
return re.sub(r'\s', '#30', s)
方法二:
def replace_space(s):
len_s = len(s)
index = 0
array = [''] * (len_s * 3)
for i in range(len_s):
if s[i] =='':
array[index] = '#'
array[index + 1] = '3'
array[index + 2] = '0'
index += 3
else:
array[index] = s[i]
index += 1
return ''.join(array[:index])