Python What Does 'u' Mean in Front of a String - codingem.com
文章推薦指數: 80 %
The 'u' in front of a string means the string is a Unicode string. A Unicode is a way for a string to represent more characters than a regular ASCII string can. SkiptocontentThe‘u’infrontofastringmeansthestringisaUnicodestring.AUnicodeisawayforastringtorepresentmorecharactersthanaregularASCIIstringcan.InPython2.xshell,youcanseehowforexampleatextwritteninRussianconvertstoaUnicodestringwith‘u’infrontofitandabunchofUnicodecharactersinit:>>>hello_rus=u"Привет,мир" >>>hello_rus u'\u041f\u0440\u0438\u0432\u0435\u0442,\u043c\u0438\u0440'InPython2.x,aUnicodestringismarkedwith‘u’.However,inPython3allstringsareUnicodestringsbydefault.Thusyouwillneversee‘u’infrontofaUnicodestringinPython3.HistoryofUnicodeValuesinPython2InPython2,thestringtypestrisagroupof8-bitcharacters.Using8-bitcharacters,itisonlypossibletorepresenttheEnglishalphabet.Butothercharacters,suchastheRussianCyrillicalphabet,cannotberepresentedby8-bitcharacters.Thisisbecauseusing8bitsmeansthereareonly255differentvaluesavailable.IfyoucombinetheEnglishalphabet,RussianCyrillicalphabet,andaccentedWesternEuropeancharacters,youwillquicklyfindouttherearemorethan255characterscombined.TorepresentcharactersoutsideoftheEnglishalphabet,Unicodecharactersareused.Inshort,Unicodemapseachcharactertoaspecificcodecalledacodepoint.Thesecodepointscanbeconvertedtobytesequenceswithavarietyofdifferentencodings.(OnecommonsuchencodingisUTF-8).Forexample,let’screateaUnicodestringwithaSpanishñ(inPython2.xshell):>>>word=u"ElNiño" >>>word u'ElNi\xf1o'Whenyoudisplaytheword,thePythoninterpreterescapestheSpanishñletteranddisplays\xf1instead.Thisisbecausetheletterñisnotinthestandardprintablerange.ThusitmustbereplacedwithsomethingthatthePythoninterpretercanunderstand.ToseethisescapedUnicodecharacter,historicallyyouwouldneedtoviewitwithasystemthatsupportsdisplayingit.ButbecausetheUnicodeencodingisbackwardcompatible,youcanviewtheUnicodecharactersintheoldersystemstoo.InPython2.x,youcansimplyprinttheUnicodestringusingtheprintstatementtodisplaytheUnicodecharacter.Justnoticethatthiswasnotpossiblebackintheday.>>>printword ElNiñoNoticehowinPython3.xnoneofthismatters.AllthestringsareUnicodestringsbydefault.ConclusionTodayyoulearnedwhatisthe‘u’infrontofastringinolderversionsofPython.Torecap,the‘u’infrontofastringmeansthestringisaUnicodestring.Thisallowsyoutoworkwithawiderangeofcharacters,suchasRussianCyrilliccharacters.However,thisonlyappliestoPython2.x.InPython3.xallthestringsareUnicodestringsforyourconvenience.Thanksforreading.Happycoding!FurtherReadingPythonTricksHowtoWritetoaFileinPythonThewithStatementinPythonPostnavigation←PreviousPostNextPost→LeaveaCommentYouremailaddresswillnotbepublished.Requiredfieldsaremarked*Typehere..Name*E-mail*Website Searchfor: Search reportthisadAbouttheAuthorHi,I'mArtturiJalli!I'maTechenthusiastfromFinland.ImakeCoding&Techeasyandfunwithwell-thoughthow-toguidesandreviews.I'vealreadyhelped2M+visitorsreachtheirgoals!Contact13BestAIArtGeneratorsof2022(Free&Paid)AIistakingover.WiththelatestadvancementsinAIartgeneration,youcancreatephotorealisticimagesoutofthinair.Thebestpartisyoudon’t...ContinueReadingHowtoMakeanApp—AComplete10-StepGuide[in2022]Areyoulookingtocreatethenextbest-sellerapp?Orareyoucuriousabouthowtocreateasuccessfulmobileapp?Thisisastep-by-stepguideon...ContinueReading9BestGraphicDesignCourses+Certification[in2022]Doyouwanttobecomeaversatileandskilledgraphicdesigner?Thisisacomprehensivearticleonthebestgraphicdesigncertificationcourses.Thesecoursesprepareyou...ContinueReading8BestPythonCourseswithCertifications[in2022]AreyoulookingtobecomeaprofessionalPythondeveloper?Orareyouinterestedinprogrammingbutdon’tknowwheretostart?Pythonisabeginner-friendlyandversatile...ContinueReading8BestSwift&iOSAppDevelopmentCourses[in2022]AreyoulookingtobecomeaniOSdeveloper?Doyouwanttocreateappswithanoutstandingdesign?Doyouwanttolearntocode?IOSApp...ContinueReadingHowtoStartaProgrammingBlog:ABeginner’sGuide[2022]Startingaprogrammingblogisagreatwaytoshareyourexpertiseandgetyourvoiceheard.Italsooffersyouachancetobuildanice...ContinueReadingRecentPosts13BestAIArtGeneratorsof2022(Free&Paid)AIGlossary:25+TermsforBeginners(in2022)9BestAIColorizersof2022—ColorizeBlack&WhiteImagesBestWebDesignSoftwareof2022(Ranked&Reviewed)7BestAIStoryGeneratorsof2022(Fiction,Novels,ShortStories)CategoriesArtificialIntelligenceCrypto&NFTDataScienceFavoritesiOSDevelopmentJavaScriptProgrammingProgrammingTipsPythonPythonforBeginnersSoftwareSwiftSwiftforBeginnersTechnologyWebdevelopmentx
延伸文章資訊
- 1python中將\\uxxxx轉換為Unicode字串的方法- IT閱讀
這篇文章主要介紹了python中將\\uxxxx轉換為Unicode字串的方法,小編覺得挺不錯的, ... return _compile(pattern, flags).sub(repl, s...
- 2瞭解Unicode — Python Tutorial 0.1 說明文件
與外界溝通- decode與encode¶. 在上面我們學到了如何表示unicode 字串,但是事實上是, unicode 字串只能存在程式的內部,並沒有 ...
- 3Python 的編碼
在Python 2.x,程式中所有字串,其實都是原始位元組集合。如果原始碼中寫了非ASCII 字元串, ... 為了支援Unicode,Python 2.x 提供了 u 前置字來產生 unico...
- 4Python What Does 'u' Mean in Front of a String - codingem.com
The 'u' in front of a string means the string is a Unicode string. A Unicode is a way for a strin...
- 5Python 中的原始字串和Unicode 字串 - Delft Stack
區分'u' 和'r' 字串標誌並瞭解原始字串文字在python 中的工作原理。