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 中Django安装和使用教程详解

Python 中Django安装和使用教程详解

  一、安装     一般使用cmd 安装就可以   手动安装通过下载方式    django官方网站:https://www.djangoproject.com/    python官...

Python计算一个文件里字数的方法

本文实例讲述了Python计算一个文件里字数的方法。分享给大家供大家参考。具体如下: 这段程序从所给文件中找出字数来。 from string import * def countW...

python简单实现操作Mysql数据库

用python编写数据库的代码很方便,但是如果不想自己写sql语句,其实还有更多的讨巧办法。使用webpy的db库就是不错的一个选择。当然为了使用webpy的db,之前你还需要安装MyS...

python3模块smtplib实现发送邮件功能

python3模块smtplib实现发送邮件功能

本文实例为大家分享了python3 smtplib发送邮件的具体代码,供大家参考,具体内容如下 smtplib模块是smtp简单邮件传输协议客户端的实现,为了通用性,有时候发送邮件的时候...

Django1.3添加app提示模块不存在的解决方法

使用Django添加应用的时候,一直提示Error: No module named myapp。意思是找不到这个名字的应用,可是我已经startapp成功,并且系统已经创建相应的目录...