python和大多数语言一样,字符串属于不可变对象,不支持原字符串修改,一般如果要修改都采用创建新字符串的方式,但是,有些时候我们就想修改原字符串,这时可以使用io.StringIO对象
#导入io模块
import io
string="hello world"
iostring=io.StringIO(string)
#获取iostring值
iostring.getvalue()
#'hello world'
#seek表示移动游标,下面代码iostring.seek(11)表示将游标移动到"hello world"的最后
iostring.seek(11)
#修改字符串
iostring.write("!")
iostring.getvalue()
#'hello world!'