How to delete a character from a string using Python

前端 未结 16 1378
耶瑟儿~
耶瑟儿~ 2020-11-22 16:35

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. I want to know:

相关标签:
16条回答
  • 2020-11-22 17:27

    UserString.MutableString

    Mutable way:

    import UserString
    
    s = UserString.MutableString("EXAMPLE")
    
    >>> type(s)
    <type 'str'>
    
    # Delete 'M'
    del s[3]
    
    # Turn it for immutable:
    s = str(s)
    
    0 讨论(0)
  • 2020-11-22 17:29

    You can simply use list comprehension.

    Assume that you have the string: my name is and you want to remove character m. use the following code:

    "".join([x for x in "my name is" if x is not 'm'])
    
    0 讨论(0)
  • 2020-11-22 17:29

    If you want to delete/ignore characters in a string, and, for instance, you have this string,

    "[11:L:0]"

    from a web API response or something like that, like a CSV file, let's say you are using requests

    import requests
    udid = 123456
    url = 'http://webservices.yourserver.com/action/id-' + udid
    s = requests.Session()
    s.verify = False
    resp = s.get(url, stream=True)
    content = resp.content
    

    loop and get rid of unwanted chars:

    for line in resp.iter_lines():
      line = line.replace("[", "")
      line = line.replace("]", "")
      line = line.replace('"', "")
    

    Optional split, and you will be able to read values individually:

    listofvalues = line.split(':')
    

    Now accessing each value is easier:

    print listofvalues[0]
    print listofvalues[1]
    print listofvalues[2]
    

    This will print

    11

    L

    0

    0 讨论(0)
  • 2020-11-22 17:31

    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   # //2 in python 3
    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.

    0 讨论(0)
  • 2020-11-22 17:32

    This is probably the best way:

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

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

    0 讨论(0)
  • 2020-11-22 17:34
    card = random.choice(cards)
    cardsLeft = cards.replace(card, '', 1)
    

    How to remove one character from a string: Here is an example where there is a stack of cards represented as characters in a string. One of them is drawn (import random module for the random.choice() function, that picks a random character in the string). A new string, cardsLeft, is created to hold the remaining cards given by the string function replace() where the last parameter indicates that only one "card" is to be replaced by the empty string...

    0 讨论(0)
提交回复
热议问题