Given the following string:
mystring = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
The goal is to swap out a character position range with other characters.
For example, swap out characters 20-24 with ABCDE.
The result would look like:
XXXXXXXXXXXXXXXXXXXABCDEXXXXXXXXXXXXXXX
Testing:
mystring = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
mystring[20:24] = 'ABCDE'
I get the error: TypeError: 'str' object does not support item assignment
The end goal is a reusable function such as:
def replace_chars(some_string, start_char, end_char, replace_string):
if len(replace_string) == (end_char_pos - start_char_pos + 1):
some_string[start_char:end_char] = replace_string
else:
print "replace string invalid length"
sys.exit(1)
return mystring
new_string = replace_chars('XYZXYZ', 2, 4, 'AAA')
I realize that it's possible to pad out the unchanged range into a new string:
mystring = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
mystring = mystring[0:19] + 'ABCDE' + mystring[25:38]
However that will force more calculation and since this will be happening thousands of times against lines in a file. The different lines will be different length and will be different character positions to swap. Doing this seems like it would be a long workaround where I should just be able to insert direct into the character positions in-place.
Appreciate any help, thanks!
-
Is this code meant to be used in biology?Back2Basics– Back2Basics2018年04月06日 23:14:27 +00:00Commented Apr 6, 2018 at 23:14
4 Answers 4
strings are immutable (unchangeable). But you can index and join items.
mystring = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
mystring = 'ABCDE'.join([mystring[:20],mystring[24:]])
'XXXXXXXXXXXXXXXXXXXXABCDEXXXXXXXXXXXXXX'
Do be careful as the string length "ABCDE" and the number of items you omit between mystring[:20], mystring[24:] need to be the same length.
6 Comments
''.join is faster than string concatenation for long strings.mystring = 'ABCDE'.join([mystring[:20],mystring[24:]])join() to pad the replacement string. Very similar to my workaround but cleaner. Will also add safety checks to ensure the replacement matches the character range.Strings are immutable in python! You'll have to split the string into three pieces and concatenate them together :)
mystring = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
new_str = "ABCDE"
first_piece = mystring[0:20]
third_piece = mystring[24:len(mystring)]
final_string = first_piece + new_str + third_piece
1 Comment
This is not strictly possible in python, but consider using bytearray a similar structure to a string in python, with a key difference being mutability
In [52]: my_stuff = bytearray('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
In [53]: my_stuff = my_stuff[0:19] + "abcd" + my_stuff[25:38]
In [54]: print my_stuff
XXXXXXXXXXXXXXXXXXXabcdXXXXXXXXXXXXX
There are some key things you should know when using a bytearray, you can see some of them here
Comments
As much as you think you should be able to assign to individual characters of a string, 'str' object does not support item assignment says you can't.