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

相关文章

python通过floor函数舍弃小数位的方法

本文实例讲述了python通过floor函数舍弃小数位的方法。分享给大家供大家参考。具体分析如下: python中可以通过math库的floor函数来舍弃浮点数后面的小数位 impo...

利用python为运维人员写一个监控脚本

利用python为运维人员写一个监控脚本

前言: 一直想写一个监控方面的脚本,然后想到了运维这方面的,后来就写了个脚本,下面话不多说了,来一起看看详细的介绍吧。 准备: psutil模块(基本使用方法可以参考这篇文章:/post...

Python实现二分查找与bisect模块详解

前言 其实Python 的列表(list)内部实现是一个数组,也就是一个线性表。在列表中查找元素可以使用 list.index() 方法,其时间复杂度为O(n) 。对于大数据量,则可以用...

详解Python匿名函数(lambda函数)

匿名函数lambda Python使用lambda关键字创造匿名函数。所谓匿名,意即不再使用def语句这样标准的形式定义一个函数。这种语句的目的是由于性能的原因,在调用时绕过函数的栈分配...

Python带动态参数功能的sqlite工具类

本文实例讲述了Python带动态参数功能的sqlite工具类。分享给大家供大家参考,具体如下: 最近在弄sqlite和python 在网上参考各教程后,结合以往java jdbc数据库工...