Python formatted string

Source: Internet
Author: User

Python supports the output of formatted strings. Although this may use a very complex expression, the most basic usage is to insert a value into a string that has the string format of%s.Shanghai Python Training-Shanghai Shang Academy editorial writing.


In the age of artificial intelligence, Python precedes!
Python vs. C: Formatting stringsIn Python, string formatting uses the same syntax as the sprintf function in C. (Do you remember sprintf in C language?) Don't remember to ask Baidu)
Example 1. Formatting of strings>>> k = "UID" >>> v = "sa" >>> "%s=%s"% (k, v) (1) ' Uid=sa '
(1) The value of the entire expression is a string. The first%s is replaced by the value of the variable K, and the second%s is replaced by the value of V. All other characters in the string (in this case, the equals sign) are printed out as-is.
Note (k, v) is a tuple. I said they were useful for something. You may have been thinking that doing so much work is just to make a simple string connection. You want to be nice, but string formatting is more than just a connection. It's not even just formatting. It is also a coercion type conversion.
Example 2. String Formatting vs. String joins
>>> uid = "sa" >>> pwd = "secret" >>> print pwd + "is not a good password for" + UID (1) Secret Is isn't a good password for sa>>> print '%s is not a good password for%s '% (pwd, UID) (2) secret is not a good PA ssWOrd for sa>>> UserCount = 6>>> print "Users connected:%d"% (UserCount,) (3) (4) Users connected:6& gt;>> print "Users connected:" + usercount (5) Traceback (innermost last): File "<interactive input>", line 1, In? Typeerror:cannot concatenate ' str ' and ' int ' objects
(1) + is a string connection operator.
(2) In this simple example, string formatting implements the same result as a join.
(3) (UserCount,) is a tuple that contains only one element. Yes, the syntax is a bit strange, but the reason to use it is to show that it is a tuple, not the other. In fact, when you define a list, tuple, or dictionary, you can always follow a comma after the last element, but a comma is required when you define a tuple that contains only one element. If you omit commas, Python will not know whether (UserCount) is a tuple with only one element or a value of variable UserCount.
(4) String formatting the integer can be processed by replacing%s with%d.
(5) Attempting to concatenate a string with a non-string will throw an exception. Unlike string formatting, a string connection can only work if each of the concatenated strings is a string. Like printf's role in C, the string formatting in Python is a Swiss Army knife. It has a wide range of options, with different format and optional modifiers available for different data types.
Example 3. Formatting of numeric values>>> print "Today ' s stock price:%f"% 50.4625 (1) 50.462500>>> print "Today's stock price:%.2f"% 50.462 5 (2) 50.46>>> print "Change since yesterday:%+.2f"% 1.5 (3) +1.50
(1) The%f format symbol option corresponds to a decimal floating-point number, which prints 6 decimal places when no precision is specified.
(2) using the%f format option with the ". 2" correction modifier will print only 2 decimal places.
(3) You can even use a mix of modifiers. The Add + modifier is used to display a positive or negative sign before the value. Note the ". 2" modifier is still in its original position and is used to print only 2 decimal places.


Welcome to read the Shanghai Python training article for more content recommendations

"Shanghai python Training" in Python all objects

"Shanghai python Training" dumb way to learn Python notes and # of Python series

Python function declaration and comparison with other programming language data types

What is python like in "Shanghai Python training"? Am I fit to learn python? 》

Python formatted string

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.