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

相关文章

Google开源的Python格式化工具YAPF的安装和使用教程

Google开源的Python格式化工具YAPF的安装和使用教程

目前用于Python的格式化程序(如autopep8和pep8ify)都用于删除代码中的lint错误。这有很明显的局限性。 YAPF采用了不同的方法,基于Daniel Jasper开发的...

Python 随机生成中文验证码的实例代码

python代码复制代码 代码如下: # -*- coding: utf-8 -*-  import Image,ImageDraw,ImageFont &nbs...

django框架使用方法详解

django框架使用方法详解

我的文章的意义 服务端开发,python,django这些内容上面的链接中有详细的阐述. 我写的内容肯定没有上面的完备,准确. 我的文章的价值在于从一个iOS程序员的角度来理解服务端开...

python3人脸识别的两种方法

python3人脸识别的两种方法

本文实例为大家分享了python3实现人脸识别的具体代码,供大家参考,具体内容如下 第一种: import cv2 import numpy as np filename = 't...

对tensorflow 的模型保存和调用实例讲解

我们通常采用tensorflow来训练,训练完之后应当保存模型,即保存模型的记忆(权重和偏置),这样就可以来进行人脸识别或语音识别了。 1.模型的保存 # 声明两个变量 v1 = t...