3
In Python it is often necessary to use str.replace(a,b)
, however .replace
is rather long and feels unnecessary. So is there a shorter alternative to str.replace
.
Alternatively is there a similar trick to how you can use r=range
for golfing for str.replace
?
Example code for golfing
m=input()
f="hello"
print m.replace('t','Ushd')
print m.replace('h','gyhj')
print f.replace('o','%£&')
1
r=str.replace; print r('abc', 'b', 'c')
? – kirbyfan64sos – 2015-10-09T20:17:30.533@kirbyfan64sos Nope, Python doesn't work like that unfortunately :( – Beta Decay – 2015-10-09T20:20:27.623
2Uh, yes it does. I just tried it now in Python 2, Python 3, Skulpt, and PyPy.js. – kirbyfan64sos – 2015-10-09T20:25:08.657
2@BetaDecay How many downvotes do you need until you believe it? – flawr – 2015-10-09T20:26:45.607
@kirbyfan64sos Well whaddaya know, that's brilliant, thanks – Beta Decay – 2015-10-09T20:27:20.647
@flawr -15 once again! ;P – Maltysen – 2015-10-09T20:27:25.040
I don't think this is necessarily a duplicate. Aliasing the function is a possible solution, but there might be others using different functions or methods. – xnor – 2015-10-09T21:01:15.543
@xnor True, but the second half of the question is just asking for a dupe of the question I linked. If it is removed, I will retract my vote – FryAmTheEggman – 2015-10-09T21:04:31.890