6

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!

asked Apr 6, 2018 at 22:54
1
  • Is this code meant to be used in biology? Commented Apr 6, 2018 at 23:14

4 Answers 4

8

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.

answered Apr 6, 2018 at 23:01
Sign up to request clarification or add additional context in comments.

6 Comments

@ScottHunter I haven't timed it, but I imagine that ''.join is faster than string concatenation for long strings.
Does that take into account the time spent making the list?
If this is for biology then this is the wrong approach. There is libraries for this specifically.
If you're worried about performance, you could get a slight improvement by skipping the list and doing mystring = 'ABCDE'.join([mystring[:20],mystring[24:]])
This seems to be the cleanest method, really like the 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.
|
2

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
Scott Hunter
50k12 gold badges65 silver badges107 bronze badges
answered Apr 6, 2018 at 22:58

1 Comment

Isn't this just what OP posted as a "workaround" without naming the first and third pieces?
0

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

answered Apr 6, 2018 at 23:35

Comments

-4

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.

answered Apr 6, 2018 at 22:58

2 Comments

I think they realize that they can't. They're looking for a solution that is allowed, but will produce the result they want.
"I should just be able to insert direct into the character positions in-place" suggests otherwise. OP has a solution to produce the desired result, but doesn't like its mechanics.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.