这篇文章主要介绍了Python原始字符串与Unicode字符串操作符用法,结合实例形式分析了Python针对原始字符与Unicode字符的操作符用法,需要的朋友可以参考下
本文实例讲述了Python原始字符串与Unicode字符串操作符用法。分享给大家供大家参考,具体如下:
#coding=utf8 ''''' 在原始字符串里,所有的字符串都是直接按照字面的意思来使用, 没有转义特殊或不能打印的字符。 正则表达式是一些告诫搜索匹配方式的字符串, 通过是由代表字符、分组、匹配信息、变量名、字符类等的特殊符号组成。 在原始字符串紧靠第一个引号前,需要加上r或R字母,来表示该字符是原始字符串。 原始字符串和普通字符串有这几乎完全相同的语法。 Unicode字符串操作符,大写的U和小写的u是在Python1.6中和Unicode字符串一起被引入的。 它用来把标准字符串或者包含Unicode字符的字符串转换成完全的Unicode字符串对象。 字符串方法和正则表达式引擎也支持Unicode。 Unicode操作符也可以接受原始Unicode字符串,只要将Unicode操作符和原始字符串操作符连接在一起。 注意:Unicode操作符必须出现在原始字符串操作符前面。 ''' import re #原始操作符函数 def originalOper(): #不是原始字符表示换行符 print "\n" #是原始字符表示\n print r"\n" try: #对文件路径加r,不需要对\进行转义 #文件路径中表示斜线 testFile=open(r"E:\PythonDemo\CorePythonBook2\output","r") for line in testFile.readlines(): #通过格式化字符串 #优先使用repr()函数转换为原始字符 line="%r" %line #对匹配模式进行加r处理 #匹配的字符转换为原始字符 m=re.search(r"\\[rn]",line) #字符串中是否包含匹配字符 #如果包含输出该字符串 if m is not None: print line except Exception,e: print e finally: testFile.close() #Unicode操作符函数 def unicodeOper(): print u"abc" print u"\u1234" print u"abc\u1234\n" print ur"Hello\nWorld!" #调用函数 #输出操作原始字符结果 originalOper() #输出Unicode字符串 unicodeOper()
运行结果如下:
Atas ialah kandungan terperinci 关于Python中原始字符串与Unicode字符串操作符的实例. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Pythonusesahybridmodelofcompilationandinterpretation: 1) thepythoninterpretercompilessourcodcecodeintoplatform-independentbytecode.2) thepythonvirtualmachine (PVM) thenexecutesthisbytecode, BalantingeaseOfusoWithperformance.

Pythonisbothinterpretedandandcompiled.1) it'scompiledtobytecodeforporabilityAcrossplatforms.2) theBytecodeistheninterpreted, membolehkanfordynamictypingandrapiddevelopment, walaupunItmayBeslowerLowerWanLelyCiledlanguages.

ForloopsareidealwhenyonesshenumberofiterationsationseSinadvance, whilewhileloopsarebetterforsituationshipheryouneedtoloopuntilaconditionismet.forloopsaremoreeficientablyandable, yang sesuai, manakala whileloopsoffermorecontrolandareusefereficeficeficeficeficient,

Forloopsareusedwhenthenumberofiterationsisknowninadvance, whilewhileloopsareusedwhenTheiterationsdependonacondition.1) forloopsareidealforiteratingoversequencesLikeListsorArrays.2)

Pythonisnotpurelyinterinterpreted; itusesahybridapproachofbytecodecompilationandruntimeinterpretation.1) pythoncompilessourcecodeintobytecode, whoomeSthenexecutedbythepythonvirtualmachine (pvm)

ToConcatenatelistsinpythonwiththesameelements, gunakan: 1) operatortokokduplicates, 2) asettoremoveduplicates, OR3) listomprehensionfensionfensionfensionfensiontroloverduplicates, setiapmethodhasdifferentperformanceAdordlications.

Pythonisaninterpretedlanguage, menawarkanfuseofuseandflexibilitybutfacingperpormancelimitationsincriticalapplications.1) interpretlanguagesepythonexecuteline-by-line, membolehkanMmediateDebackandrapidprototyping.2)

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit


Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

Video Face Swap
Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Artikel Panas

Alat panas

Versi Mac WebStorm
Alat pembangunan JavaScript yang berguna

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

mPDF
mPDF ialah perpustakaan PHP yang boleh menjana fail PDF daripada HTML yang dikodkan UTF-8. Pengarang asal, Ian Back, menulis mPDF untuk mengeluarkan fail PDF "dengan cepat" dari tapak webnya dan mengendalikan bahasa yang berbeza. Ia lebih perlahan dan menghasilkan fail yang lebih besar apabila menggunakan fon Unicode daripada skrip asal seperti HTML2FPDF, tetapi menyokong gaya CSS dsb. dan mempunyai banyak peningkatan. Menyokong hampir semua bahasa, termasuk RTL (Arab dan Ibrani) dan CJK (Cina, Jepun dan Korea). Menyokong elemen peringkat blok bersarang (seperti P, DIV),

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

PhpStorm versi Mac
Alat pembangunan bersepadu PHP profesional terkini (2018.2.1).
