问题:’str’对象不支持Python中的项目分配
我想从字符串中读取一些字符,然后将其放入其他字符串中(就像我们在C语言中一样)。
所以我的代码如下
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)
在C中,代码可能是
i = j = 0;
while(str[i] != '\0')
{
srr[j++] = str [i++];
}
如何在Python中实现相同的功能?
I would like to read some characters from a string and put it into other string (Like we do in 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
i = j = 0;
while(str[i] != '\0')
{
srr[j++] = str [i++];
}
How can I implement the same in Python?
回答 0
在Python中,字符串是不可变的,因此您不能就地更改其字符。
但是,您可以执行以下操作:
for i in str:
srr += i
起作用的原因是它是以下操作的快捷方式:
for i in str:
srr = srr + i
上面的代码在每次迭代时都会创建一个新字符串,并将对该新字符串的引用存储在中srr
。
In Python, strings are immutable, so you can’t change their characters in-place.
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
.
回答 1
其他答案是正确的,但是您当然可以执行以下操作:
>>> str1 = "mystring"
>>> list1 = list(str1)
>>> list1[5] = 'u'
>>> str1 = ''.join(list1)
>>> print(str1)
mystrung
>>> type(str1)
<type 'str'>
如果您真的想要。
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.
回答 2
Python字符串是不可变的,因此您在C语言中尝试执行的操作在python中根本不可能实现。您将必须创建一个新字符串。
我想从字符串中读取一些字符,然后将其放入其他字符串中。
然后使用字符串切片:
>>> s1 = 'Hello world!!'
>>> s2 = s1[6:12]
>>> print s2
world!
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.
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!
回答 3
如aix所述-Python中的字符串是不可变的(您不能就地更改它们)。
您要尝试执行的操作可以通过多种方式完成:
# 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[:]
As aix mentioned – strings in Python are immutable (you cannot change them inplace).
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[:]
回答 4
如果您想将特定字符换成另一个字符,则可以采用另一种方法:
def swap(input_string):
if len(input_string) == 0:
return input_string
if input_string[0] == "x":
return "y" + swap(input_string[1:])
else:
return input_string[0] + swap(input_string[1:])
Another approach if you wanted to swap out a specific character for another character:
def swap(input_string):
if len(input_string) == 0:
return input_string
if input_string[0] == "x":
return "y" + swap(input_string[1:])
else:
return input_string[0] + swap(input_string[1:])
回答 5
该解决方案如何:
str =“ Hello World”(如问题所述)srr = str +“”
How about this solution:
str=”Hello World” (as stated in problem)
srr = str+ “”
回答 6
嗨,您应该尝试使用字符串拆分方法:
i = "Hello world"
output = i.split()
j = 'is not enough'
print 'The', output[1], j
Hi you should try the string split method:
i = "Hello world"
output = i.split()
j = 'is not enough'
print 'The', output[1], j