Professional Python by Sneeringer Luke
Author:Sneeringer, Luke [Sneeringer, Luke]
Language: eng
Format: azw3, epub
ISBN: 9781119070788
Publisher: Wiley
Published: 2015-10-06T16:00:00+00:00
Python 2 Strings
Python 2 strings mostly work similarly, but with some subtle but very important distinctions.
The first distinction is the name of the classes. The Python 3 str class is called unicode in Python 2. In and of itself, this is fine. However, the Python 3 bytes class is called str in Python 2. This means that a Python 3 str is a text string, whereas a Python 2 str is a byte string. If you are using Python 2, it is critically important to understand this distinction.
Instantiating a string with no prefix gives you a str (remember, this is a byte string!) instance.
>>> byte_str = 'The quick brown fox jumped over the lazy dogs.' >>> type(byte_str) <type 'str'>
If you want a text string in Python 2, you prefix the string literal with the u character, as shown here:
>>> text_str = u'The quick brown fox jumped over the lazy dogs.' >>> type(text_str) <type 'unicode'>
Unlike Python 3, Python 2 does attempt to implicitly convert between text strings and byte strings. The way that this works is that if the interpreter encounters a mixed operation, it will first convert the byte string to a text string, and then perform the operation against the text strings.
It works this way so that an operation against a byte string and a text string will return a text string:
>>> 'foo' + u'bar' u'foobar'
The interpreter performs this implicit decoding using whatever the default encoding is. On Python 2, this is almost always ASCII. Python defines a method, sys.getdefaultencoding, which provides the default codec for implicitly converting between text strings and byte strings.
>>> import sys >>> sys.getdefaultencoding() 'ascii'
This means that many of the previous Python 3 examples show distinctly different behavior in Python 2.
>>> 'foo' == u'foo' True >>> >>> d = {u'foo': u'bar'} >>> d['foo'] u'bar'
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(12570)
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9796)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Dependency Injection in .NET by Mark Seemann(9337)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8296)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7070)
Microservices with Go by Alexander Shuiskov(6831)
Practical Design Patterns for Java Developers by Miroslav Wengner(6750)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6693)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Angular Projects - Third Edition by Aristeidis Bampakos(6097)
The Art of Crafting User Stories by The Art of Crafting User Stories(5622)
NetSuite for Consultants - Second Edition by Peter Ries(5560)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5367)
Kotlin in Action by Dmitry Jemerov(5062)
