“str”对象不支持Python中的项目赋值。

时间:2022-03-10 00:32:21

I would like to read some characters from a string and put it into other string (Like we do in C).

我想从字符串中读取一些字符并将其放入其他字符串中(就像我们在C中所做的那样)。

So my code is like below

我的代码如下。

import string
import re
str = "Hello World"
j = 0
srr = ""
for i in str:
    srr[j] = i #'str' object does not support item assignment 
    j = j + 1
print (srr)

In C the code may be

在C中,代码可能是。

i = j = 0; 
while(str[i] != '\0')
{
srr[j++] = str [i++];
}

How can I implement the same in Python?

我如何在Python中实现相同的功能?

6 个解决方案

#1


49  

In Python, strings are immutable, so you can't change their characters in-place.

在Python中,字符串是不可变的,所以不能就地修改它们的字符。

You can, however, do the following:

不过,你可以这样做:

for i in str:
    srr += i

The reasons this works is that it's a shortcut for:

其原因是,这是一条捷径:

for i in str:
    srr = srr + i

The above creates a new string with each iteration, and stores the reference to that new string in srr.

上面的内容将在每个迭代中创建一个新的字符串,并将引用存储到srr中的新字符串。

#2


58  

The other answers are correct, but you can, of course, do something like:

其他的答案都是正确的,但你当然可以做一些类似的事情:

>>> str1 = "mystring"
>>> list1 = list(str1)
>>> list1[5] = 'u'
>>> str1 = ''.join(list1)
>>> print(str1)
mystrung
>>> type(str1)
<type 'str'>

if you really want to.

如果你真的想。

#3


8  

Python strings are immutable so what you are trying to do in C will be simply impossible in python. You will have to create a new string.

Python字符串是不可变的,所以您在C中尝试做的事情在Python中是不可能的。您将不得不创建一个新的字符串。

I would like to read some characters from a string and put it into other string.

我想从字符串中读取一些字符并将其放入其他字符串中。

Then use a string slice:

然后使用一个字符串片:

>>> s1 = 'Hello world!!'
>>> s2 = s1[6:12]
>>> print s2
world!

#4


4  

As aix mentioned - strings in Python are immutable (you cannot change them inplace).

正如aix所提到的,Python中的字符串是不可变的(您不能更改它们的位置)。

What you are trying to do can be done in many ways:

你想要做的事情可以在很多方面做:

# Copy the string

foo = 'Hello'
bar = foo

# Create a new string by joining all characters of the old string

new_string = ''.join(c for c in oldstring)

# Slice and copy
new_string = oldstring[:]

#5


-1  

How about this solution:

这个解决方案:

str="Hello World" (as stated in problem) srr = str+ ""

"Hello World"(如问题所述)srr = str+ ""

#6


-3  

Hi you should try the string split method:

你好,你应该试试字符串分割方法:

i = "Hello world"
output = i.split()

j = 'is not enough'

print 'The', output[1], j

#1


49  

In Python, strings are immutable, so you can't change their characters in-place.

在Python中,字符串是不可变的,所以不能就地修改它们的字符。

You can, however, do the following:

不过,你可以这样做:

for i in str:
    srr += i

The reasons this works is that it's a shortcut for:

其原因是,这是一条捷径:

for i in str:
    srr = srr + i

The above creates a new string with each iteration, and stores the reference to that new string in srr.

上面的内容将在每个迭代中创建一个新的字符串,并将引用存储到srr中的新字符串。

#2


58  

The other answers are correct, but you can, of course, do something like:

其他的答案都是正确的,但你当然可以做一些类似的事情:

>>> str1 = "mystring"
>>> list1 = list(str1)
>>> list1[5] = 'u'
>>> str1 = ''.join(list1)
>>> print(str1)
mystrung
>>> type(str1)
<type 'str'>

if you really want to.

如果你真的想。

#3


8  

Python strings are immutable so what you are trying to do in C will be simply impossible in python. You will have to create a new string.

Python字符串是不可变的,所以您在C中尝试做的事情在Python中是不可能的。您将不得不创建一个新的字符串。

I would like to read some characters from a string and put it into other string.

我想从字符串中读取一些字符并将其放入其他字符串中。

Then use a string slice:

然后使用一个字符串片:

>>> s1 = 'Hello world!!'
>>> s2 = s1[6:12]
>>> print s2
world!

#4


4  

As aix mentioned - strings in Python are immutable (you cannot change them inplace).

正如aix所提到的,Python中的字符串是不可变的(您不能更改它们的位置)。

What you are trying to do can be done in many ways:

你想要做的事情可以在很多方面做:

# Copy the string

foo = 'Hello'
bar = foo

# Create a new string by joining all characters of the old string

new_string = ''.join(c for c in oldstring)

# Slice and copy
new_string = oldstring[:]

#5


-1  

How about this solution:

这个解决方案:

str="Hello World" (as stated in problem) srr = str+ ""

"Hello World"(如问题所述)srr = str+ ""

#6


-3  

Hi you should try the string split method:

你好,你应该试试字符串分割方法:

i = "Hello world"
output = i.split()

j = 'is not enough'

print 'The', output[1], j