Skip to content

Tag: replace

pythonic way to replace two character in a string

I have a string ,for example s = “-1+2-3” I want to replace all – to + and all + to -. what I expected is s = +1-2+3 .So I can’t just use s.replace(‘-‘,’+’).replace(‘+’,’-‘),because it return s=-1-2-3,maybe a for loop can help. But I wonder if there is a pythonic way to do so? thanks for all solutions i

Put comma after a pattern in python regex

Just like the question says I am trying to add a comma at the end of a pattern or sub string. I found 3 solutions that should do the job, and look logical too. But they are not changing anything. I will show you all those codes. The goal is to find out if there is something that I am

Trying to filter out A-Z and special characters in python

This is what I’ve tried so far: It doesn’t work, can you explain why? Answer Should work. This code will check with is_alpha if the i character is an alphabetic letter (A-Z, case unsensitive) and if it returns false, will add it to the new_string variable. Later, it will check if the string contains the characters to remove. Check information