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

yipeiwu_com6年前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程序设计有所帮助。

相关文章

Python切图九宫格的实现方法

Python切图九宫格的实现方法

本文介绍了Python切图九宫格的实现方法,分享给大家,具体如下 # -*- coding: utf-8 -*- ''' 将一张图片填充为正方形后切为9张图 ''' from PI...

Python实现使用dir获取类的方法列表

使用Python的内置方法dir,可以范围一个模块中定义的名字的列表。 官方解释是: Docstring: dir([object]) -> list of strings...

Python进程间通信之共享内存详解

前一篇博客说了怎样通过命名管道实现进程间通信,但是要在windows是使用命名管道,需要使用python调研windows api,太麻烦,于是想到是不是可以通过共享内存的方式来实现。查...

python使用range函数计算一组数和的方法

本文实例讲述了python使用range函数计算一组数和的方法。分享给大家供大家参考。具体如下: sum = 0 numbers = range(1,10) for i in num...

Python占用的内存优化教程

概述 如果程序处理的数据比较多、比较复杂,那么在程序运行的时候,会占用大量的内存,当内存占用到达一定的数值,程序就有可能被操作系统终止,特别是在限制程序所使用的内存大小的场景,更容易发...