Python的字符串被划分为不可变序列的类别,意味着这些字符串所包含的字符存在从左至右的位置顺序,并且它们不可以在本地进行修改。
基本操作
字符串可以通过+操作符进行合并,可以使用*运算符进行重复。
>>>len("abc") 3 >>>'abc'+'def' 'abcdef' >>>'NI!'*4 'NI!NI!NI!NI!'
字符串内部的一个反斜杠“\”可允许把字符串放于多行。
>>>str = "aaa\ ....bbb\ ....ccc\ ....ddd" >>>str aaabbbcccddd
索引和分片
在Python中,字符串中的字符是通过索引提取的。
分片X[I:J],表示“取出在X中从偏移量为I,直到但不包括偏移量为J的内容”。结果就是返回一个新的对象。
在一个分片中,左边界默认为0,右边界默认为分片序列的长度。
S = 'Spam' >>>S[1:] 'pam' >>>S 'Spam' >>>S[:3] 'Spa' >>>S[:-1] 'Spa' >>>S[:] 'Spam' S[:]实现了一个完全的顶层的序列对象的拷贝-一个有相同值,但是是不同内存片区的对象。 X[I:J:K]表示“索引x对象中的元素,从偏移为I直到偏移为J-1,每隔K元素索引一次“,第三个限制K,默认为1,表示步进。 也可以使用负数作为步进,S[::-1]的作用实际上是将序列进行发转。 >>>S = 'hello' >>>S[::-1] 'olleh'
字符串转化工具
int函数将字符串转换为数字,str函数将数字转换为字符串表达形式。repr函数也能够将一个对象转换为其字符串形式,然后这些返回的对象将作为代码的字符串,可以重新创建对象。
不可变性
字符串属于不可变序列,即不能在原地修改一个字符串,例如,给一个索引进行赋值。若要改变一个字符串,需要利用合并,分片这样的工具来建立并赋值给一个新的字符串,倘若必要的话,还要将这个结果赋值给字符串最初的变量名。
>>>S = 'spam' >>>S[0] = "x" #不允许修改S的值 >>>S = S + "SPAM" >>>S 'spamSPAM' >>>S = 'splot' >>>S = S.replace('pl', 'plmal') >>>S 'splmalot'

本篇文章给大家带来了关于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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),