Python 简明教程

Python - Modify Strings

String modification 指的是更改字符串字符的过程。如果我们谈论修改 Python 中的字符串,那我们讨论的是创建一个作为原始内容变化的新字符串。

String modification refers to the process of changing the characters of a string. If we talk about modifying a string in Python, what we are talking about is creating a new string that is a variation of the original one.

Python 中,一个 stringstr 类对象)是不可变类型。此处,不可变是指内存中创建对象后无法对其就地进行修改的对象。不同于 list ,我们无法覆盖序列中的任何字符,也不能直接在其后插入或追加字符。如果我们需要修改字符串,我们将使用返回新字符串对象的某些字符串方法。但是,原始字符串保持不变。

In Python, a string (object of str class) is of immutable type. Here, immutable refers to an object that cannot be modified in place once it’s created in memory. Unlike a list, we cannot overwrite any character in the sequence, nor can we insert or append characters to it directly. If we need to modify a string, we will use certain string methods that return a new string object. However, the original string remains unchanged.

我们可以使用以下任意技巧作为解决方法来修改字符串。

We can use any of the following tricks as a workaround to modify a string.

Converting a String to a List

Python 中的字符串和列表都是序列类型,它们是可以相互转换的。因此,我们可以将字符串转换成列表,使用列表中诸如 insert()append()remove() 等方法对其进行修改,然后将列表转换成字符串,以获得修改版本。

Both strings and lists in Python are sequence types, they are interconvertible. Thus, we can cast a string to a list, modify the list using methods like insert(), append(), or remove() and then convert the list back to a string to obtain a modified version.

假设我们有一个字符串变量 s1,它的值为 WORD,我们要求将它转换成列表。对于此操作,我们可以使用 list() 内置函数,并在索引 3 位置插入字符 L。然后,我们可以使用 str 类中的 join() 方法连接所有字符。

Suppose, we have a string variable s1 with WORD as its value and we are required to convert it into a list. For this operation, we can use the list() built-in function and insert a character L at index 3. Then, we can concatenate all the characters using join() method of str class.

Example

以下示例实际说明了如何将字符串转换为列表。

The below example practically illustrates how to convert a string into a list.

s1="WORD"
print ("original string:", s1)
l1=list(s1)

l1.insert(3,"L")

print (l1)

s1=''.join(l1)
print ("Modified string:", s1)

它将生成以下 output

It will produce the following output

original string: WORD
['W', 'O', 'R', 'L', 'D']
Modified string: WORLD

Using the Array Module

要修改字符串,请使用名为数组模块的 Python 标准库构建 array object 。它将从字符串 variable 中创建一个 Unicode 类型的数组。

To modify a string, construct an array object using the Python standard library named array module. It will create an array of Unicode type from a string variable.

Example

在以下示例中,我们使用数组模块来修改指定字符串。

In the below example, we are using array module to modify the specified string.

import array as ar

# initializing a string
s1="WORD"
print ("original string:", s1)

# converting it to an array
sar=ar.array('u', s1)

# inserting an element
sar.insert(3,"L")

# getting back the modified string
s1=sar.tounicode()
print ("Modified string:", s1)

它将生成以下 output

It will produce the following output

original string: WORD
Modified string: WORLD

Using the StringIO Class

Python 的 io 模块定义了处理流的类。StringIO 类使用内存中的文本缓冲区表示文本流。从字符串获得的 StringIO 对象表现得像一个文件对象。因此,我们可以在其上执行读/写操作。StringIO 类的 getvalue() 方法返回一个字符串。

Python’s io module defines the classes to handle streams. The StringIO class represents a text stream using an in-memory text buffer. A StringIO object obtained from a string behaves like a File object. Hence we can perform read/write operations on it. The getvalue() method of StringIO class returns a string.

Example

让我们在以下程序中使用上述讨论的原则来修改字符串。

Let us use the above discussed principle in the following program to modify a string.

import io

s1="WORD"
print ("original string:", s1)

sio=io.StringIO(s1)
sio.seek(3)
sio.write("LD")
s1=sio.getvalue()

print ("Modified string:", s1)

它将生成以下 output

It will produce the following output

original string: WORD
Modified string: WORLD