python将字符串转换成数组的方法

python将字符串转换成数组的方法。分享给大家供大家参考。具体实现方法如下:

#—————————————–
# name: string_to_array.py
# author: kevin harris
# last modified: 02/13/04
# description: this python script demonstrates
# how to modify a string by
# converting it to an array
#—————————————–
import array
str = ‘my name is kevin’
print( ‘str = ‘ + str )
# we’re not allowed to modify strings
# so we’ll need to convert it to a
# character array instead…
chararray = array.array( ‘b’, str )
# assignment
chararray[11:16] = array.array( ‘b’, ‘jason’ )
# replacement
str = chararray.tostring()
# assignment back to the string object
print( ‘str = ‘ + str )
input( ‘\n\npress enter to exit…’ )

输出结果:

str = my name is kevin
str = my name is jason
press enter to exit…

希望本文所述对大家的python程序设计有所帮助。

Posted in 未分类