- The Python Apprentice
- Robert Smallshire Austin Bingham
- 107字
- 2021-07-02 22:16:57
Raw strings
Sometimes, particularly when dealing with strings such as Windows filesystem paths or regular expression patterns which use backslashes extensively, the requirement to double-up on backslashes can be ugly and error prone. Python comes to the rescue with its raw strings. Raw strings don't support any escape sequences and are very much what-you-see-is-what-you-get. To create a raw string, precede the opening quote with a lower-case r:
>>> path = r'C:\Users\Merlin\Documents\Spells'
>>>
>>> path
'C:\\Users\\Merlin\\Documents\\Spells'
>>> print(path)
C:\Users\Merlin\Documents\Spells
Although it's common to store and manipulate filesystem paths as strings, for anything but the most straightforward path handling, you should investigate the Python Standard Library pathlib module.
推薦閱讀
- UML和模式應用(原書第3版)
- Java 9 Concurrency Cookbook(Second Edition)
- Visual FoxPro程序設計教程
- 算法零基礎一本通(Python版)
- 營銷數據科學:用R和Python進行預測分析的建模技術
- SAP BusinessObjects Dashboards 4.1 Cookbook
- 軟件測試綜合技術
- HTML+CSS+JavaScript網頁制作:從入門到精通(第4版)
- Mastering Object:Oriented Python(Second Edition)
- CryENGINE Game Programming with C++,C#,and Lua
- Manage Your SAP Projects with SAP Activate
- C語言程序設計
- Mastering R for Quantitative Finance
- TensorFlow.NET實戰
- 微信公眾平臺服務號開發:揭秘九大高級接口