rsplit and join could be used to simulate the effects of an rreplace
>>> 'XXX'.join('mississippi'.rsplit('iss', 1))
'missXXXippi'
Answer from Stephen Emslie on Stack Overflowpython replace characters in string from left to right - Stack Overflow
Python: How can I replace one specific character on a string while leaving the rest of the string as it was?
python - How to remove the left part of a string? - Stack Overflow
Python f string notation for setting margins of printed outputs question
You could put the translations into a dict, and also combine the search-patterns into a single regular expression, which gives priority to the longer patterns. Then use the callback argument that re.sub accepts to make the replacement using the dict.
import re
trans = {
"00": "a",
"11": "b",
"01": "c",
"10": "d",
"0000": "e",
"1111": "f",
"0101": "g",
"1010": "h"
}
regex = "|".join(sorted(trans.keys(), key=len, reverse=True))
# demo
s = "0000000110110110100111111111"
result = re.sub(regex, lambda x: trans[x.group(0)], s)
print(result) # eacdbchcff
Non-regex approach would be to assess each section as a set of 4 characters, see if theres a match for those, or split into two halves of the 4 and get a match for them...
replacements = {'0000': 'e', '1111': 'f', '1010': 'h', '0101': 'g', '10': 'd', '01': 'c', '11': 'b', '00': 'a'}
s = "0000000110110110100111111111"
r_d = replacement_dict # only here to shorten comprehension
for i in range(0, len(s), 4):
print(r_d.get(s[i:i+4], r_d.get(s[i:i+2], "") +r_d.get(s[i+2:i+4],"")), end="")
or with loop as a list comprehension
"".join(r_d.get(s[i:i+4], r_d.get(s[i:i+2], "") +r_d.get(s[i+2:i+4], "")) for i in range(0, len(s), 4))
'eacdbcddcff'
word1 = input("Word: ") # lets say that the given word is "tower"
word2 = "********************"
word2 = word2.replace(word[3], word[3])
print(word2)
# Now the my code replaces all the "*" characters with the character "e"
# It just prints "eeeeeeeeeeeeeeeeeeee"
# I would like the code only to replace the 4th character with the other strings 4th
# I'd like it to print "***e****************"
If the string is fixed you can simply use:
if line.startswith("Path="):
return line[5:]
which gives you everything from position 5 on in the string (a string is also a sequence so these sequence operators work here, too).
Or you can split the line at the first =:
if "=" in line:
param, value = line.split("=",1)
Then param is "Path" and value is the rest after the first =.
Remove prefix from a string
# ...
if line.startswith(prefix):
return line[len(prefix):]
Split on the first occurrence of the separator via str.partition()
def findvar(filename, varname="Path", sep="=") :
for line in open(filename):
if line.startswith(varname + sep):
head, sep_, tail = line.partition(sep) # instead of `str.split()`
assert head == varname
assert sep_ == sep
return tail
Parse INI-like file with ConfigParser
from ConfigParser import SafeConfigParser
config = SafeConfigParser()
config.read(filename) # requires section headers to be present
path = config.get(section, 'path', raw=1) # case-insensitive, no interpolation
Other options
str.split()re.match()