The new string formatting method in Python3.x

Source: Internet
Author: User
This article mainly introduces the new string formatting method in Python3.x. This article compares the example code of the string formatting method in Python2.x and Python3.x, for more information, see Python3.x. Different from Python2.x's

The code is as follows:


"% S" % (a, B)


Python3.x is

The code is as follows:


"{0} {1}". format (a, B)


When I use MySQLdb today

The code is as follows:


Cursor.exe cute (SQL, param)


Statement to complete the SQL operation. It was misled by the old saying in other articles and used

The code is as follows:


Cursor.exe cute ('Insert into test values (% s, % s, % s) ', param)


Param is a tuples that indicate the data to be inserted. each element in the tuples is the value of each column in the database.
However, a database exception is always thrown during execution. error message:

The code is as follows:


[1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(% s, % s, % s) 'At line ...... (Omitted below ).


In fact, you just need to change it to this:

The code is as follows:


Cursor.exe cute ('Insert into test values ({0}, {1}, {2}) ', param)


To be honest, Python3 is incompatible with every pitfall of Python2, increasing the cost of learning Python3. But in order not to impede the development of science and technology and social progress, I decided to join the new version ......

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.