Shortening str.replace()

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','%£&')

Beta Decay

Posted 2015-10-09T20:15:11.813

Reputation: 21 478

Question was closed 2015-10-09T23:38:48.713

1r=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

Answers

4

r=str.replace;print r('abc','b','c')

The magic is real.

Shortening str.replace()

kirbyfan64sos

Posted 2015-10-09T20:15:11.813

Reputation: 8 730

3

Regex Replace

You can use re.sub, but it will take several uses to pay off if you're only doing it one character at a time.

import re
s="hello"
r=s.replace('h','m')
r=re.sub('h','m',s)

Translate

You may also wish to look at string.translate, which can be used for large numbers of single-character replacements. Requires Python 3. If using Python 2, maketrans is required first. For an example, see here. In Python 3, it's used like this (taking a literal dictionary of ASCII values):

print("hello".translate({104:109}))

Output:

mello

mbomb007

Posted 2015-10-09T20:15:11.813

Reputation: 21 944

You don't need to use maketrans, try "hello".translate({104:105}). This is a trick some python golfers have used in the past – FryAmTheEggman – 2015-10-09T21:03:11.400

@FryAmTheEggman Ah right. I always forget you can do that, since the documentation doesn't show that anywhere. Thanks. – mbomb007 – 2015-10-09T21:07:21.470

@FryAmTheEggman Just realized that requires Python 3. Edited answer to reflect that. – mbomb007 – 2016-01-27T17:45:00.863

Also re.sub fails if you use regex metachars. – CalculatorFeline – 2016-03-09T01:04:58.293

@CatsAreFluffy No it doesn't, you just have to escape them as a regex string like r'\' or with a backslash like '\\'. – mbomb007 – 2016-03-09T14:29:36.683

Though it becomes pointless if your search is r"\.\.\.\.\.\.\.\." Too many backslashes. (Also r"something" is called a raw string.) – CalculatorFeline – 2016-03-09T15:09:10.480

@CatsAreFluffy The entire premise of the question was to replace a single character at a time. And if that's your search, you'd do string multiplication. – mbomb007 – 2016-03-09T15:58:07.447