Python path--python should I format the string using% or format?

Source: Internet
Author: User

One,%, or format1,%, Format Imperial PK

The format string in Python currently has two camps:% and format, which one should we choose?

Since Python2.6 introduced the format-formatted string method, I think the% or format is not a problem at all. Don't believe you look down.

# define a coordinate value c = (+)#  use% to format " enemy Coordinates:%s" % c

The above code obviously throws a typeerror like this:

Typeerror:not all arguments converted during string formatting

Like this kind of formatting needs we need to write the following ugly format:

# define a coordinate value c = (+)#  Use the% ugly formatting ... " enemy Coordinates:%s " % (c,)

There is no problem with format:

# define a coordinate value c = (in.)#  format " Enemy Coordinates: {}"with format. Format ( C

Obviously, the above reason is enough for you to use format for future projects.

2. New Features

Added f-strings to Python3.6:

" Q1mi " in[2]: Age =in[3]: F'My name is {name}. I ' m {age}"out["My name is Q1mi.i ' m "

second, the usual format usage1, through the location
IN[1]: data = ["q1mi",]in["name:{0}, age:{1}". Format (*data) out['name:q1mi, age:18'

2, through the key words
IN[1]: data = {"name""Q1mi" "Age" :}in["name:{name}, age:{age}". Format (* *data) out['  name:q1mi, age:18'

3. Through object Properties
IN[1]:classPerson (Object): ...:def __init__(self, Name, age): ...: Self.name=name ...: self.age=Age ...:def __str__(self): ...:return "This was {self.name}, {self.age}, years old.". Format (self=Self ) ...: in[2]: p = person ("Q1mi", 18) in[3]: str (p) out[3]:'This was q1mi, years old.'

4, through the subscript
" {0[0]} is {0[1]} years old. " . Format (data) out['q1mi is the years old. '

5. Fill and alignpadding is used in conjunction with alignment
    • ^, <, > center, Align Left, right, back with width
    • : The filled character after the number, only one character, not specified by default is filled with spaces.
IN[1]:"{: >10}". Format (' -') out[1]:' -'in[2]:"{: 0>10}". Format (' -') out[2]:'0000000018'in[3]:"{: a>10}". Format (' -') out[3]:'AAAAAAAA18

Add a string that comes with the Zfill () method:

The Python Zfill () method returns a string of the specified length, the original string is right-aligned, and the front padding is 0. Zfill () method Syntax: Str.zfill (width)parameter width Specifies the length of the string. The original string is right-aligned with the front padding 0. returns a string of the specified length.
"  - ". Zfill (Ten) out['0000000018'

6. Accuracy and type F

Accuracy is often used in conjunction with Type F.

" {:. 2f} ". Format (3.1415926) out['3.14'

Where. 2 represents a precision of 2 length, and F represents a float type.

7. Other binary

B, D, O, and X are binary, decimal, octal, hexadecimal, respectively.

IN[1]:"{: b}". Format (18) out[1]:'10010'in[2]:"{:D}". Format (18) out[2]:' -'in[3]:"{: o}". Format (18) out[3]:' A'in[4]:"{: x}". Format (18) out[4]:' A'

8. Thousand separators
" {:,} ". Format (1234567890) out['1,234,567,890'

Python path--python should I format the string using% or format?

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.