日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Python程序:替換字符串中的字符

創(chuàng)新互聯(lián)Python教程:

用替換函數(shù)和 For 循環(huán)編寫一個(gè) Python 程序來(lái)替換字符串中的字符。

替換字符串 1 中字符的 Python 程序

該程序允許用戶輸入字符串、要替換的字符和要替換的新字符。接下來(lái),我們使用一個(gè)名為 replace 的內(nèi)置字符串函數(shù),用一個(gè)新字符替換用戶給定的字符。

# Python program to Replace Characters in a String

str1 = input("Please Enter your Own String : ")
ch = input("Please Enter your Own Character : ")
newch = input("Please Enter the New Character : ")

str2 = str1.replace(ch, newch)

print("\nOriginal String :  ", str1)
print("Modified String :  ", str2)

替換字符串的程序示例 2

在這個(gè)程序中,我們使用 For 循環(huán)來(lái)迭代字符串中的每個(gè)字符。在 For 循環(huán)中,我們使用 If 語(yǔ)句來(lái)檢查字符串字符是否等于 ch。如果為真, Python 替換為 Newch

# Python program to Replace Characters in a String

str1 = input("Please Enter your Own String : ")
ch = input("Please Enter your Own Character : ")
newch = input("Please Enter the New Character : ")

str2 = ''
for i in range(len(str1)):
    if(str1[i] == ch):
        str2 = str2 + newch
    else:
        str2 = str2 + str1[i]

print("\nOriginal String :  ", str1)
print("Modified String :  ", str2)

Python 替換字符串輸出

Please Enter your Own String : tutorial gateway team
Please Enter your Own Character : t
Please Enter the New Character : P

Original String :   tutorial gateway team
Modified String :   PuPorial gaPeway Peam

Python 替換字符串中的字符示例 3

這個(gè) Python 替換字符串字符的代碼與上面的例子相同。然而,我們使用的是對(duì)象循環(huán)。

# Python program to Replace Characters in a String

str1 = input("Please Enter your Own String : ")
ch = input("Please Enter your Own Character : ")
newch = input("Please Enter the New Character : ")

str2 = ''
for i in str1:
    if(i == ch):
        str2 = str2 + newch
    else:
        str2 = str2 + i

print("\nOriginal String :  ", str1)
print("Modified String :  ", str2)

Python 替換字符串輸出

Please Enter your Own String : python programming examples
Please Enter your Own Character : o
Please Enter the New Character : G

Original String :   python programming examples
Modified String :   pythGn prGgramming examples

分享標(biāo)題:Python程序:替換字符串中的字符
當(dāng)前鏈接:http://www.dlmjj.cn/article/djeesoo.html