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 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...
- 2python中將\\uxxxx轉換為Unicode字串的方法- IT閱讀
這篇文章主要介紹了python中將\\uxxxx轉換為Unicode字串的方法,小編覺得挺不錯的, ... return _compile(pattern, flags).sub(repl, s...
- 3Python 中的原始字串和Unicode 字串 - Delft Stack
區分'u' 和'r' 字串標誌並瞭解原始字串文字在python 中的工作原理。
- 4Python diving — Unicode 深入淺出 - Medium
1. Python unicode & bytes. 這邊先說一下Python3 裡的字串可以用兩個型別來表示:(1) str (2) ...
- 5[Python][Python3] 字串前加u、r、b 的意義 - 葛瑞斯肯樂活筆記
Python 3 中的編碼預設就是unicode,不過在字串前加上u,依舊可以強迫轉換後續字串為unicode: #將字串編碼成unicode str1 = u"今天天氣很好"