TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

'in-place' string modifications in Python

1 pointsby sea6earover 1 year ago

1 comment

necovekover 1 year ago
As pointed out, strings are immutable and depending on F(c) being mostly c or not, different approaches might work best.<p>Eg. for a single replacement (I haven&#x27;t seen this one mentioned), slicing might work best: s[:i-1] + F(s[i]) + s[i+1:]. This is obviously good for a small number of replacements too, though you might want to switch to &quot;&quot;.join(iterable) for better performance.<p>In general, immutability of strings requires a more functional approach than in-place editing.