Changing one character in a string

前端 未结 11 806
抹茶落季
抹茶落季 2020-11-22 03:21

What is the easiest way in Python to replace a character in a string?

For example:

text = \"abcdefg\";
text[1] = \"Z\";
           ^
相关标签:
11条回答
  • 2020-11-22 03:56

    This code is not mine. I couldn't recall the site form where, I took it. Interestingly, you can use this to replace one character or more with one or more charectors. Though this reply is very late, novices like me (anytime) might find it useful.

    Change Text function.

    mytext = 'Hello Zorld'
    mytext = mytext.replace('Z', 'W')
    print mytext,
    
    0 讨论(0)
  • 2020-11-22 04:01

    Python strings are immutable, you change them by making a copy.
    The easiest way to do what you want is probably:

    text = "Z" + text[1:]
    

    The text[1:] returns the string in text from position 1 to the end, positions count from 0 so '1' is the second character.

    edit: You can use the same string slicing technique for any part of the string

    text = text[:1] + "Z" + text[2:]
    

    Or if the letter only appears once you can use the search and replace technique suggested below

    0 讨论(0)
  • 2020-11-22 04:02

    Starting with python 2.6 and python 3 you can use bytearrays which are mutable (can be changed element-wise unlike strings):

    s = "abcdefg"
    b_s = bytearray(s)
    b_s[1] = "Z"
    s = str(b_s)
    print s
    aZcdefg
    

    edit: Changed str to s

    edit2: As Two-Bit Alchemist mentioned in the comments, this code does not work with unicode.

    0 讨论(0)
  • 2020-11-22 04:08

    if your world is 100% ascii/utf-8(a lot of use cases fit in that box):

    b = bytearray(s, 'utf-8')
    # process - e.g., lowercasing: 
    #    b[0] = b[i+1] - 32
    s = str(b, 'utf-8')
    

    python 3.7.3

    0 讨论(0)
  • 2020-11-22 04:08

    I would like to add another way of changing a character in a string.

    >>> text = '~~~~~~~~~~~'
    >>> text = text[:1] + (text[1:].replace(text[0], '+', 1))
    '~+~~~~~~~~~'
    

    How faster it is when compared to turning the string into list and replacing the ith value then joining again?.

    List approach

    >>> timeit.timeit("text = '~~~~~~~~~~~'; s = list(text); s[1] = '+'; ''.join(s)", number=1000000)
    0.8268570480013295
    

    My solution

    >>> timeit.timeit("text = '~~~~~~~~~~~'; text=text[:1] + (text[1:].replace(text[0], '+', 1))", number=1000000)
    0.588400217000526
    
    0 讨论(0)
  • 2020-11-22 04:10

    Fastest method?

    There are three ways. For the speed seekers I recommend 'Method 2'

    Method 1

    Given by this answer

    text = 'abcdefg'
    new = list(text)
    new[6] = 'W'
    ''.join(new)
    

    Which is pretty slow compared to 'Method 2'

    timeit.timeit("text = 'abcdefg'; s = list(text); s[6] = 'W'; ''.join(s)", number=1000000)
    1.0411581993103027
    

    Method 2 (FAST METHOD)

    Given by this answer

    text = 'abcdefg'
    text = text[:1] + 'Z' + text[2:]
    

    Which is much faster:

    timeit.timeit("text = 'abcdefg'; text = text[:1] + 'Z' + text[2:]", number=1000000)
    0.34651994705200195
    

    Method 3:

    Byte array:

    timeit.timeit("text = 'abcdefg'; s = bytearray(text); s[1] = 'Z'; str(s)", number=1000000)
    1.0387420654296875
    
    0 讨论(0)
提交回复
热议问题