新聞中心
創(chuàng)新互聯(lián)python教程:

海港ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
寫一個(gè) Python 程序,用替換函數(shù)用字符串中的連字符替換空格,用一個(gè)實(shí)際例子替換 For 循環(huán)。
用連字符替換字符串中空格的 Python 程序示例 1
這個(gè) python 程序允許用戶輸入一個(gè)字符串。接下來(lái),我們使用內(nèi)置的替換字符串函數(shù)用連字符替換空白。
# Python program to Replace Blank Space with Hyphen in a String
str1 = input("Please Enter your Own String : ")
str2 = str1.replace(' ', '_')
print("Original String : ", str1)
print("Modified String : ", str2)Python 用連字符輸出替換字符串中的空格
Please Enter your Own String : Hello World Program
Original String : Hello World Program
Modified String : Hello_World_Program用連字符替換字符串中空格的 Python 程序示例 2
在這個(gè)程序中,我們使用 For 循環(huán)來(lái)迭代字符串中的每個(gè)字符。在 For 循環(huán)中,我們使用 If 語(yǔ)句來(lái)檢查字符串字符是空的還是空格。如果是真的, Python 會(huì)用 _。
# Python program to Replace Blank Space with Hyphen in a String
str1 = input("Please Enter your Own String : ")
str2 = ''
for i in range(len(str1)):
if(str1[i] == ' '):
str2 = str2 + '_'
else:
str2 = str2 + str1[i]
print("Modified String : ", str2)Python 用連字符輸出替換字符串中的空格
Please Enter your Own String : Hello World!
Modified String : Hello_World!
>>>
Please Enter your Own String : Python program Output
Modified String : Python_program_Output用連字符替換字符串中空格的 Python 程序示例 3
這個(gè) Python 用連字符替換空格的程序與上面的例子相同。然而,我們使用的是對(duì)象循環(huán)。
# Python program to Replace Blank Space with Hyphen in a String
str1 = input("Please Enter your Own String : ")
str2 = ''
for i in str1:
if(i == ' '):
str2 = str2 + '_'
else:
str2 = str2 + i
print("Modified String : ", str2) 本文題目:Python 程序:用字符串中的連字符替換空格
鏈接URL:http://www.5511xx.com/article/dhohdse.html


咨詢
建站咨詢
