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真是太动态了,所有的方法默认都是虚的。子类定义父类同名函数之后,父类函数被覆盖。 cla...

Python计算程序运行时间的方法

本文实例讲述了Python计算程序运行时间的方法。分享给大家供大家参考。具体实现方法如下: 复制代码 代码如下: import time def start_sleep():  ...

对python 调用类属性的方法详解

对python 调用类属性的方法详解

测试时候类的调用是经常会用到的。简单看下类的调用使用的方法吧。 来看例子: 目录结构: 我们现在要在do_class.py这个文件里调用class_learn.py里的类 代码(do_...

python中ConfigParse模块的用法

本文实例讲述了python中ConfigParse模块的用法,分享给大家供大家参考。具体方法如下: 写配置一般用ConfigParse.RawConfigParse类 读配置用Conf...

用python读写excel的方法

本文实例讲述了用python读写excel的方法。分享给大家供大家参考。具体如下: 最近需要从多个excel表里面用各种方式整理一些数据,虽然说原来用过java做这类事情,但是由于最近在...