简体   繁体   English

如何替换字符串中的多个字符 Python

[英]How do I replace more than one character in a string Python

Sorry in advance, I'm barely beginning in programming.提前抱歉,我才刚刚开始编程。

I'm struggling with this program more than I thought I would.我在这个程序上的挣扎比我想象的要多。 Take a look :看看

string = str(input("Enter a string: "))

delimiter = input("Enter delimiters: ")


s = list(string)

d = list(delimiter)


def split(string, delimiter):

    for i in s:
        if i in d:
            x = string.replace(i, " ")

    print(x)


split(string, delimiter)

And the output I'm supposed to get is:我应该得到的 output 是:

Enter a string: Welcome to Python

Enter delimiters: oe
W lc m t Pyth n

Here comes the problem: if I enter more than 1 character, the program will only pass the last character I entered and will ignore the others.问题来了:如果我输入超过 1 个字符,程序只会传递我输入的最后一个字符,而忽略其他字符。

Here is the output I'm getting:这是我得到的 output:

 Enter a string: Welcome to Python

 Enter delimiters: oe
 Welc me t  Pyth n

I will appreciate any help given!我将不胜感激任何帮助! Thanks in advance!!!提前致谢!!!

the parameter string is passed a copy and doesnt change by its self so every time you set x = string.replace(i, " ").参数字符串被传递一个副本并且不会自行更改,因此每次设置 x = string.replace(i, " ") 时。 you are once again getting the original string and replacing only the current character i.您将再次获得原始字符串并仅替换当前字符 i。

this should do the trick:这应该可以解决问题:

def split(string, delimiter):
    x = string
    for i in s:
    if i in d:
        x = string.replace(i, " ")

    print(x)

You can use re.sub() for your question:您可以使用re.sub()来解决您的问题:

import re

def split(string, delimiter):
    pattern = "[" + delimiter + "]"
    new_string = re.sub(pattern, " ", string)
    return new_string

string = str(input("Enter a string: "))
delimiter = input("Enter delimiters: ")

split(string, delimiter)

Daniel.丹尼尔。 The answer from DSMSTHN is correct but there is an additional note for you. DSMSTHN 的答案是正确的,但还有一个额外的说明。 str doesn't need to be converted into list because it is already iterable. str 不需要转换为 list 因为它已经是可迭代的。 So, here is my version of code.所以,这是我的代码版本。

string = input("Enter a string: ")
delimiter = input("Enter delimiters: ")


def split(string, delimiter):
    for s in string:
        if s in delimiter:
            string = string.replace(s, " ")
    print(string)


split(string, delimiter)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python如何用多个字符分割字符串? - python how to split string with more than one character? "Python - 如何使用 slice 方法、find 和 rfind 在 python 上删除多个字符" - Python - How do i delete more than one character on python using slice method, find and rfind 如何将numpy数组元素设置为一个以上字符的字符串? - How do I set a numpy array element to a string of more than one character? 如何用 Python 中的另一个字符替换字符串中的一个字符? - How do I replace a character in a string with another character in Python? Python-如何将包含多个数字的字符串转换为int? - Python - How do I convert a a string containing more than one number to an int? 如何在字符串中找到多于一个子字符串的位置(Python 3.4.3 shell) - How do i find the position of MORE THAN ONE substring in a string (Python 3.4.3 shell) 当 Python 中有多个文件路径时,正则表达式替换字符串中的文件路径 - Regex to replace filepaths in a string when there's more than one in Python 如何用范围替换字符串中的字符? - how do i replace a character in a string by range? 如何从 Python 中的多个来源生成随机字符? - How to generate random character from more than one source in Python? Python 使用多个字符拆分字符串,同时仍保留该字符 - Python Split a String using more than one character while still keeping that character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM