Examples of raw string and Unicode string operators in Python
这篇文章主要介绍了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()
运行结果如下:
The above is the detailed content of Examples of raw string and Unicode string operators in Python. For more information, please follow other related articles on the PHP Chinese website!

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.