Change first occurrence of character x in a string - how?

Ned Batchelder ned at nedbatchelder.com
Sun Feb 14 16:36:52 EST 2021


with_at = with_dots.replace(".", "@", 1)

https://docs.python.org/3/library/stdtypes.html#str.replace

--Ned.

On Sunday, February 14, 2021 at 4:18:22 PM UTC-5, Chris Green wrote:
> What's the easiest way to change the first occurrence of a specified 
> character in a string? 
> 
> E.g. I want to change linux-raid.vger.kernel.org to 
> linux... at vger.kernel.org, it's a fairly general requirement of 
> needing to change '.' to '@'. 
> 
> Alternatively is there an RE 'match' function that would test if 
> linux... at vger.kernel.org matches linux-raid.vger.kernel.org? I don't 
> really care if the '.' are all regarded as wild cards, the match will 
> be accurate enough. 
> 
> -- 
> Chris Green 
> ·


More information about the Python-list mailing list