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使用类(class)和对象(object),进行面向对象(object-oriented programming,简称OOP)的编程。 面向对象的最主要目的是提高程序的重复使...

Python实现八皇后问题示例代码

八皇后问题描述 问题: 国际象棋棋盘是8 * 8的方格,每个方格里放一个棋子。皇后这种棋子可以攻击同一行或者同一列或者斜线(左上左下右上右下四个方向)上的棋子。在一个棋盘上如果要放八个...

Python:slice与indices的用法

slice:   eg:     >>>e=[0,1,2,3,4,5,6]     >>>s=slice(2,3)     >>&...

python使用Berkeley DB数据库实例

本文实例讲述了python使用Berkeley DB数据库的方法,分享给大家供大家参考。 具体实现方法如下: try: from bsddb import db except...

python3基于TCP实现CS架构文件传输

本文实例为大家分享了python3实现CS架构文件传输的具体代码,供大家参考,具体内容如下 1、目标: 基于tcp实现CS架构的文件传输 指令列表:(1)get:从服务器端下载文件 &n...