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字符串方法分类,字符串是经常可以看到的一个数据储存类型,我们要进行字符的数理,就需要用各种的方法,这里有许多方法,我给大家介绍比较常见的重要的方法,比如填充、删减、变形、分切...

wxpython中Textctrl回车事件无效的解决方法

本文实例讲述了wxpython中Textctrl回车事件无效的解决方法。分享给大家供大家参考,具体如下: 今天使用wxptyhon的Textctrl控件开发客户端时遇到了一个问题, 按照...

在python中利用numpy求解多项式以及多项式拟合的方法

构建一个二阶多项式:x^2 - 4x + 3 多项式求解 >>> p = np.poly1d([1,-4,3]) #二阶多项式系数 >>> p...

Python面向对象程序设计构造函数和析构函数用法分析

本文实例讲述了Python面向对象程序设计构造函数和析构函数用法。分享给大家供大家参考,具体如下: 构造函数和析构函数 1、构造方法的使用 很多类都倾向于将对象创建为有初始化状态.因此类...

python如何修改装饰器中参数

本文实例为大家分享了python修改装饰器中参数的具体代码,供大家参考,具体内容如下 案例:        为分析程序内哪些...