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

相关文章

Python3+Appium安装使用教程

Python3+Appium安装使用教程

一、安装 我们知道selenium是桌面浏览器自动化操作工具(Web Browser Automation) appium是继承selenium自动化思想旨在使手机app操作也能自动化的...

Python从单元素字典中获取key和value的实例

之前写代码很多时候会遇到这么一种情况:在python的字典中只有一个key/value键值对,想要获取其中的这一个元素还要写个for循环获取。 网上搜了一下,发现还有很多简单的方法: 方...

使用coverage统计python web项目代码覆盖率的方法详解

使用coverage统计python web项目代码覆盖率的方法详解

本文实例讲述了使用coverage统计python web项目代码覆盖率的方法。分享给大家供大家参考,具体如下: 在使用python+selenium过程中,有时候考虑代码覆盖率,所以专...

python中利用await关键字如何等待Future对象完成详解

前言 本文主要给大家介绍了关于python用await关键字等待Future对象完成的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。 在下面的例子里,演示了怎...

Python实现注册、登录小程序功能

Python实现注册、登录小程序功能

主要实现功能 1、用户输入用户名,在用户名文件中查找对应的用户,若无对应用户名则打印输入错误 2、用户名输入正确后,进行密码匹配。输入密码正确则登录成功,否则重新输入。 3、连续输错三次...