python开发之字符串string操作方法实例详解
本文实例讲述了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程序设计有所帮助。

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

Pylance type detection problem solution when using custom decorator In Python programming, decorator is a powerful tool that can be used to add rows...

About Pythonasyncio...

Using python in Linux terminal...

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Compatibility issues between Python asynchronous libraries In Python, asynchronous programming has become the process of high concurrency and I/O...

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

The problem and solution of the child process continuing to run when using signals to kill the parent process. In Python programming, after killing the parent process through signals, the child process still...
