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

yipeiwu_com5年前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脚本,查询出某个目录下修改时间最新的文件。 应用场景举例:比如有时候需要...

使用Python3内置文档高效学习以及官方中文文档

概述 从前面的对Python基础知识方法介绍中,我们几乎是围绕Python内置方法进行探索实践,比如字符串、列表、字典等数据结构的内置方法,和大量内置的标准库,诸如functools、...

Python生成个性签名图片获取GUI过程解析

Python生成个性签名图片获取GUI过程解析

这篇文章主要介绍了Python生成个性签名图片获取GUI过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 先来看看程序运行的样子...

python发腾讯微博代码分享

复制代码 代码如下:import urllib.parse,os.path,time,sys,re,urllib.requestfrom http.client import HTTPS...

pymssql ntext字段调用问题解决方法

下面是调用方式: Example script - pymssql module (DB API 2.0) Example script - _mssql module (lower...