日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

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

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

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

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

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

# 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

在這個程序中,我們使用 For 循環(huán)來迭代字符串中的每個字符。在 For 循環(huán)中,我們使用 If 語句來檢查字符串字符是否等于 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

這個 Python 替換字符串字符的代碼與上面的例子相同。然而,我們使用的是對象循環(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

分享文章:Python程序:替換字符串中的字符
路徑分享:http://www.5511xx.com/article/djeesoo.html