#方法1 使用函数 def lenth1(lists): lists=lists.replace(" ","%20") # print(lists) return lists,len(lists) list1 = "Besttest andashu" b=lenth1(list1) # print(list1) print(b) # 使用for循环 def replace(s): b='' for i in range(len(s)): if s[i] ==" ": b +="%20" else: b +=s[i] return b,len(b) s="Besttest andashu" # print(len(s)) print(replace(s))