[英]How do I modify my Python code below to append a character to the beginning of the string in Pandas?
[英]How do I modify a single character in a string, in Python?
如何在Python中修改字符串中的單個字符? 就像是:
a = "hello"
a[2] = "m"
'str'對象不支持項目分配。
字符串在Python中是不可變的。 您可以使用字符列表:
a = list("hello")
當你想顯示結果時使用''.join(a)
:
a[2] = 'm'
print ''.join(a)
在python中,字符串是不可變的。 如果要更改單個字符,則必須使用切片 :
a = "hello"
a = a[:2] + "m" + a[3:]
嘗試從中構建一個列表。 當你將一個iterable傳遞給一個列表構造函數時,它會把它變成一個列表 (這有點過於簡單化了,但通常都有效)。
a = list("hello")
a[2] = m
然后,您可以使用''.join(a)
。
這是因為python中的字符串是不可變的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.