Explain in depth the strings in Python Programming

Source: Internet
Author: User
This article mainly introduces the strings in Python programming, including the formatting of strings and the use of three quotation marks. For more information, see Python escape characters
When special characters are needed, python uses a backslash (\) to escape the characters. See the following table:

Python string operators
The following table lists the instance variable a with the string "Hello" and variable B with the value "Python ":

Python string formatting
Python supports formatting string output. Although this may use a very complex expression, the most basic usage is to insert a value into a string with the string format character % s.
In Python, string formatting uses the same syntax as the sprintf function in C.
Example:

#!/usr/bin/pythonprint "My name is %s and weight is %d kg!" % ('Zara', 21) 

Output result of the above instance:

My name is Zara and weight is 21 kg!


Python string formatting symbol:

Auxiliary instructions for formatting operators:

Python three quotes (triple quotes)
You can copy complex strings with three quotation marks in python:
Python three quotes allow a string to span multiple lines. a string can contain line breaks, tabs, and other special characters.
The syntax of the three quotation marks is a pair of consecutive single or double quotation marks (usually used in pairs ).

 >>> hi = '''hi there'''>>> hi  # repr()'hi\nthere'>>> print hi # str()hi there 

The three quotation marks free programmers from the quarary of quotation marks and special strings. The so-called WYSIWYG (WYSIWYG) format keeps a small string from start to end.
A typical use case is that when you need an HTML or SQL statement, character strings can be combined to escape special strings.

 errHTML = '''Friends CGI DemoERROR%s

'''cursor.execute('''CREATE TABLE users ( login VARCHAR(8), uid INTEGER,prid INTEGER)''')

Unicode string
Defining a Unicode string in Python is as simple as defining a normal string:

>>> u'Hello World !'u'Hello World !'


The "u" in lower case before quotation marks indicates that a Unicode string is created here. If you want to add a special character, you can use the Unicode-Escape encoding of Python. For example:

>>> u'Hello\u0020World !'u'Hello World !'

The replaced \ u0020 mark indicates that a Unicode character (space character) with an encoding value of 0x0020 is inserted at a given position ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.