tags:

views:

160

answers:

7

This is for a friend and we are brand new to Python.

There is a string, for example EXAMPLE

How can I remove the middle character i.e. M from it. I don't need the code, what I want to know is

  • Do strings in python end in any special character?
  • Which is a better way - shifting everything right to left starting from the middle character OR creation of a new string and not copying the middle character?
+9  A: 

This is probably the best way:

original = "EXAMPLE"
removed = original.replace("M", "")

Don't worry about shifting characters and such. Most python takes place on a much higher level of abstraction.

recursive
@recursive: `M` might not be unique. In that case, this will replace all the `M`s, right?
Lazer
Yes, that's correct. If you only want to replace `n` occurrences, use `original.replace("M", "", n)`.
recursive
@Lazer: Try it and see.
S.Lott
+1  A: 

To replace a specific position:

s = s[:pos] + s[(pos+1):]

To replace a specific character:

s = s.replace('M','')
Eton B.
While this may work, I think your answer could be improved by explaining what is going on in the first part, since the substring operations are not necessarily easy to understand for a Python newbie without any explanation.
jloubert
+4  A: 

In Python, strings are immutable, so you have to create a new string. You have a few options of how to create the new string. If you want to remove the 'M' wherever it appears:

newstr = oldstr.replace("M", "")

If you want to remove the central character:

midlen = len(oldstr)/2
newstr = oldstr[:midlen] + oldstr[midlen+1:]

You asked if strings end with a special character. No, you are thinking like a C programmer. In Python, strings are stored with their length, so any byte value, including \0, can appear in a string.

Ned Batchelder
Given that the questioner is brand new to python, it might be worth noting that while in version 2.X python the "/" operator returns an integer (truncated towards zero), in version 3.X python you should use "//" instead. Also, the line `from __future__ import division` at the beginning of your script will make version 2.X python act like version 3.X
Michael Dunn
A: 

Strings are immutable in Python so both your options mean the same thing basically.

Skilldrick
+2  A: 

How can I remove the middle character

You can't, because strings in Python are immutable.

Do strings in python end in any special character?

No. They are similar to lists of characters; the length of the list defines the length of the string, and no character acts as a terminator.

Which is a better way - shifting everything right to left starting from the middle character OR creation of a new string and not copying the middle character?

You cannot modify the existing string, so you must create a new one containing everything except the middle character.

Richard Fearn
@Richard: thanks!
Lazer
+1  A: 

UserString.MutableString

mutable way:

import UserString

s = UserString.MutableString("EXAMPLE")

>>> type(s)
<type 'str'>

#del 'M'
del s[3]

#turn it for immutable:
s = str(s)
killown
A: 

Strings are immutable. But you can convert them to a list, which is mutable, and then convert the list back to a string after you've changed it.

s = "this is a string"

l = list(s)  # convert to list

l[1] = ""    # "delete" letter h (the item actually still exists but is empty)
l[1:2] = []  # really delete letter h (the item is actually removed from the list)
del(l[1])    # another way to delete it

p = l.index("a")  # find position of the letter "a"
del(l[p])         # delete it

s = "".join(l)  # convert back to string

You can also create a new string, as others have shown, by taking everything except the character you want from the existing string.

kindall