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

相关文章

Django框架下在视图中使用模版的方法

 打开current_datetime 视图。 以下是其内容: from django.http import HttpResponse import datetime...

Python中请使用isinstance()判断变量类型

一、isinstance() 在Python中可以使用type()与isinstance()这两个函数判断对象类型,而isinstance()函数的使用上比type更加方便。 复制代码...

从0开始的Python学习014面向对象编程(推荐)

从0开始的Python学习014面向对象编程(推荐)

简介 到目前为止,我们的编程都是根据数据的函数和语句块来设计的,面向过程的编程。还有一种我们将数据和功能结合起来使用对象的形式,使用它里面的数据和方法这种方法叫做面向对象的编程。 类和对...

python 多线程将大文件分开下载后在合并的实例

废话不多说了,上代码吧: import threading import requests import time import os class Mythread(thread...

Python使用pylab库实现画线功能的方法详解

Python使用pylab库实现画线功能的方法详解

本文实例讲述了Python使用pylab库实现画线功能的方法。分享给大家供大家参考,具体如下: pylab 提供了比较强大的画图功能,但是函数和参数都比较多,很容易搞混。我们平常使用最多...