實現(xiàn)一個函數(shù),將一個字符串中的空格替換成“%20”
#!/usr/bin/env python
# -*- coding: utf-8 -*-
test = raw_input() # 從鍵盤輸入
inital_length = len(test)
kong = 0 # kong 用來統(tǒng)計輸入的一行數(shù)據(jù)中總共有多少個空格
for i in test:
if i ==" ":
kong += 1
final_length = inital_length + 2*kong # 計算轉(zhuǎn)換后的數(shù)組的長度
p1 = inital_length - 1
p2 = final_length - 1
result = [0]*final_length # 預(yù)先設(shè)定一個新的數(shù)組
if p1 == p2:
print(test)
while p1 >= 0: # 重復(fù)此循環(huán)直到p1=0
if test[p1] != " ":
result[p2] = test[p1]
p1 = p1-1
p2 = p2 - 1
elif test[p1] == " ":
result[p2] = "0"
result[p2 - 1] = "2"
result[p2 - 2] = "%"
p1 = p1 - 1
p2 = p2 - 3
convert_str = ''.join(result) # ''.join()將一個數(shù)組轉(zhuǎn)換為一個字符串
print(convert_str)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
