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 eval函数的妙用

python eval函数功能:将字符串str当成有效的表达式来求值并返回计算结果。 函数定义: eval(expression, globals=None, locals=Non...

python基于multiprocessing的多进程创建方法

本文实例讲述了python基于multiprocessing的多进程创建方法。分享给大家供大家参考。具体如下: import multiprocessing import time...

PyQt5使用QTimer实现电子时钟

PyQt5使用QTimer实现电子时钟

本文用 PyQt5 的QTimer类的两种方式实现电子时钟,供大家参考,具体内容如下 【效果图】 【知识点】 QTimer类提供了定时器信号/槽和单触发定时器。 它在内部使用定时器事件...

Python中xrange与yield的用法实例分析

本文实例分析了Python中xrange与yield的用法。分享给大家供大家参考,具体如下: range和xrange Python提供了生成和返回整数序列的内置函数range及xran...

Python编程之变量赋值操作实例分析

本文实例讲述了Python编程之变量赋值操作。分享给大家供大家参考,具体如下: #coding=utf8 ''''' Python中主要通过等号(=)进行赋值。 Python中的赋值...