Answers for "replace multiple characters in string python"

2

python substitute multiple letters

message = "h@ll$ w$rld"
# make the replacement ('!'->'a', '@'->'e', etc.)
message.translate(str.maketrans("!@#$%", "aeiou"))
# outputs hello world
Posted by: Guest on May-26-2021
-2

replace multiple characters in a string

// Replace multiple characters in a string

fn main() {
    // Clumsy way
    let s = "The quick (brown) fox jumps over the lazy dog's back, and "runs" away; without barking: crazy.";
    let s = s.replace("(", "");
    let s = s.replace(")", "");
    let s = s.replace(",", "");
    let s = s.replace(""", "");
    let s = s.replace(".", "");
    let s = s.replace(";", "");
    let s = s.replace(":", "");
    let s = s.replace("'", "");
    println!("{}", s);

    // Better way
    let s = "The quick (brown) fox jumps over the lazy dog's back, and runs away; without barking: crazy.";
    let s = s.replace(&['(', ')', ',', '"', '.', ';', ':', '''][..], "");
    println!("{}", s);        
}
Posted by: Guest on October-13-2021

Code answers related to "replace multiple characters in string python"

Python Answers by Framework

Browse Popular Code Answers by Language