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中的eval()、exec()及其相关函数

刚好前些天有人提到eval()与exec()这两个函数,所以就翻了下Python的文档。这里就来简单说一下这两个函数以及与它们相关的几个函数,如globals()、locals()和co...

python 写入csv乱码问题解决方法

需求背景 最近为公司开发了一套邮件日报程序,邮件一般就是表格,图片,然后就是附件。附件一般都是默认写到txt文件里,但是PM希望邮件里的附件能直接用Excel这种软件打开,最开始想保存...

Windows系统下安装Python的SSH模块教程

Windows系统下安装Python的SSH模块教程

Python中使用SSH需要用到OpenSSH,而OpenSSH依赖于paramiko模块,而paramiko模块又依赖于pycrypto模块,因此要在Python中使用SSH,则需要先...

opencv3/C++ 平面对象识别&透视变换方式

opencv3/C++ 平面对象识别&透视变换方式

findHomography( ) 函数findHomography( )找到两个平面之间的透视变换H。 参数说明: Mat findHomography( InputArray...

Python实现读取并保存文件的类

本文实例讲述了Python实现读取并保存文件的类。分享给大家供大家参考,具体如下: 这个类写在一个叫class_format.py 的文件里, 放在D盘 >>> i...