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 jenkins 打包构建代码的示例代码

python jenkins 打包构建代码 # pip install python-jenkins import jenkins import pprint import tim...

利用python numpy+matplotlib绘制股票k线图的方法

利用python numpy+matplotlib绘制股票k线图的方法

一、python numpy + matplotlib 画股票k线图 # -- coding: utf-8 -- import requests import numpy as np...

Django实现图片文字同时提交的方法

本文实例讲述了Django实现图片文字同时提交的方法。分享给大家供大家参考。具体分析如下: jQuery为我们网站开发解决了很多问题,使我们的网站用户体验大大的提高了。举个简单的例子,我...

进一步探究Python中的正则表达式

字符串是编程时涉及到的最多的一种数据结构,对字符串进行操作的需求几乎无处不在。比如判断一个字符串是否是合法的Email地址,虽然可以编程提取@前后的子串,再分别判断是否是单词和域名,但这...

Python3.5 Pandas模块之Series用法实例分析

Python3.5 Pandas模块之Series用法实例分析

本文实例讲述了Python3.5 Pandas模块之Series用法。分享给大家供大家参考,具体如下: 1、Pandas模块引入与基本数据结构 2、Series的创建...