本文实例讲述了python开发之字符串string操作方法。分享给大家供大家参考,具体如下:
在python中,对于字符串string的操作,我们有必要了解一下,这样在我们的以后的开发中会给我们带来很多方便
下面是我学习的笔记:
#python-string #python中的字符串用单引号''和双引号""标示 strA = 'this is a string' strB = "this is a message!" #打印两个字符串 print("打印两个字符串") print('strA = ' + strA) print('strB = ' + strB) print("#############################") strC = 'I don\'t know anything' strD = '\'Yes\',I know.' print("字符串中的转义字符") print('strA = ' + strC) print('strB = ' + strD) print("#############################") strE = '这是我的blog,欢迎大家来\n我的博客溜达' print("字符串中的换行") print('strA = ' + strE) print("#############################") strF = 'this is ''message' strG = 'Hongten' strH = strG * 3 print('字符串可以用\'+\'号连接(或者说粘合),也可以用\'*\'号循环') print('strF原有形式为:\'this is \'\'message\'') print('粘合后的strF:' + strF) print('strG原值为:\'Hongten\',strH = strG * 3,此时strH为:' + strH) print("#############################") strI = 'hongtenzone@foxmail.com' print('字符串可以使用下标(索引)查询') print('源字符串strI = \'hongtenzone@foxmail.com\'') print('字符串strI的长度,len(strI) = ') print(len(strI)) print('strI[0] = ' + strI[0]) print('strI[10] = ' + strI[10]) print('strI[-1] = strI[len(strI) - 1]') print('strI[-1] = ' + strI[-1]) print('strI[len(strI) - 1] = ' + strI[len(strI) - 1]) print("#############################") print('Python 字符串不能改写。按字符串索引赋值会产生错误:') print('strI[0] = \'x\',这样就会产生错误啦') print("#############################") print('过大的索引代替为字符串大小,下界比上界大的返回空字符串') print('strI[0:100] = ' + strI[0:100]) print("#############################") print('索引可以是负数,计数从右边开始') print('strI[-2] = ' + strI[-2]) print('strI[-23:] = ' + strI[-23:]) print("#############################") print('不过-0 还是0,所以它不是从右边计数的!') print('strI[0] = ' + strI[0]) print('strI[-0] = ' + strI[-0])
运行效果如下:
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> 打印两个字符串 strA = this is a string strB = this is a message! ############################# 字符串中的转移字符 strA = I don't know anything strB = 'Yes',I know. ############################# 字符串中的换行 strA = 这是我的blog,欢迎大家来 我的博客溜达 ############################# 字符串可以用'+'号连接(或者说粘合),也可以用'*'号循环 strF原有形式为:'this is ''message' 粘合后的strF:this is message strG原值为:'Hongten',strH = strG * 3,此时strH为:HongtenHongtenHongten ############################# 字符串可以使用下标(索引)查询 源字符串strI = 'hongtenzone@foxmail.com' 字符串strI的长度,len(strI) = 23 strI[0] = h strI[10] = e strI[-1] = strI[len(strI) - 1] strI[-1] = m strI[len(strI) - 1] = m ############################# Python 字符串不能改写。按字符串索引赋值会产生错误: strI[0] = 'x',这样就会产生错误啦 ############################# 过大的索引代替为字符串大小,下界比上界大的返回空字符串 strI[0:100] = hongtenzone@foxmail.com ############################# 索引可以是负数,计数从右边开始 strI[-2] = o strI[-23:] = hongtenzone@foxmail.com ############################# 不过-0 还是0,所以它不是从右边计数的! strI[0] = h strI[-0] = h >>>
print打印字符串语句如下:
print('理解切片的最好方式是把索引视为两个字符之间的点,第一个字符的左边是0,字符串中第n个字符的右边是索引n') print(' +---+---+---+---+---+ ') print(' | H | e | l | p | A |') print(' +---+---+---+---+---+ ') print(' 0 1 2 3 4 5 ') print('-5 -4 -3 -2 -1 -0') print('第一行是字符串中给定的0到5各个索引的位置,第二行是对应的负索引。从i 到j 的切片由这两个标志之间的字符组成') print('对于非负索引,切片长度就是两索引的差。例如,word[1:3] 的长度是2')
运行效果如下:
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> 理解切片的最好方式是把索引视为两个字符之间的点,第一个字符的左边是0,字符串中第n个字符的右边是索引n +---+---+---+---+---+ | H | e | l | p | A | +---+---+---+---+---+ 0 1 2 3 4 5 -5 -4 -3 -2 -1 -0 第一行是字符串中给定的0到5各个索引的位置,第二行是对应的负索引。从i 到j 的切片由这两个标志之间的字符组成 对于非负索引,切片长度就是两索引的差。例如,word[1:3] 的长度是2 >>>
希望本文所述对大家Python程序设计有所帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft