#-----------------------------------------# 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 arraystr = '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 )# assignmentchararray[11:16] = array.array( 'b', 'jason' )# replacementstr = chararray.tostring()# assignment back to the string objectprint( 'str = ' + str )input( '\n\npress enter to exit...' )
输出结果:
str = my name is kevinstr = my name is jason press enter to exit...
希望本文所述对大家的python程序设计有所帮助。
