这篇文章主要给大家介绍了关于学会Python字符串处理的七种技巧,其中包括字符串的连接和合并、字符串的切片和相乘、字符串的分割、字符串的开头和结尾的处理、字符串的查找和匹配、字符串的替换以及字符串中去掉一些字符等操作,需要的朋友可以参考。
前言
日常使用python经常要对文本进行处理,无论是爬虫的数据解析,还是大数据的文本清洗,还是普通文件的处理,都是要用到字符串. Python对字符串的处理内置了很多高效的函数,非常方便功能很强大.下面是我总结的常用的7招,有了这几招就能轻松应对字符串处理.
一、字符串的连接和合并
连接和合并
相加 //两个字符串可以很方便的通过'+'连接起来
合并//用join方法
二、字符串的切片和相乘
相乘//比如写代码的时候要分隔符,用python很容易实现
line='*'*30 print(line) >>******************************
切片
三、字符串的分割
普通的分割,用split
split只能做非常简单的分割,而且不支持多个分隔
phone='400-800-800-1234' print(phone.split('-')) >>['400', '800', '800', '1234']
复杂的分割
r表示不转义,分隔符可以是;或者,或者空格后面跟0个多个额外的空格,然后按照这个模式去分割
四、字符串的开头和结尾的处理
比方我们要查一个文件的名字是以什么开头或者什么结尾
filename='trace.h' print(filename.endswith('h')) >>True print(filename.startswith('trace')) >>True
五、字符串的查找和匹配
一般查找
我们可以很方便的在长的字符串里面查找子字符串,会返回子字符串所在位置的索引, 若找不到返回-1
复杂的匹配
六、字符串的替换
普通的替换//用replace就可以
复杂的替换//若要处理复杂的或者多个的替换,需要用到re模块的sub函数
七、字符串中去掉一些字符
去除空格//对文本处理的时候比如从文件中读取一行,然后需要去除每一行的两侧的空格,table或者是换行符
line=' Congratulations, you guessed it. ' print(line.strip()) >>Congratulations, you guessed it.
注意:字符串内部的空格不能去掉,若要去掉需要用re模块
复杂的文本清理,可以利用str.translate
,
先构建一个转换表,table是一个翻译表,表示把't''o'转成大写的'T' 'O',
然后在old_str里面去掉'12345',然后剩下的字符串再经过table翻译
总结
以上是教大家Python字符串处理的七种技巧的详细内容。更多信息请关注PHP中文网其他相关文章!

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,减法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

记事本++7.3.1
好用且免费的代码编辑器

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能