Hướng dẫn what is %d %s %f in python? - % d% s% f trong python là gì?

Đây là bài tập:

Như đã nói, %s (của chuỗi) là để thay thế một chuỗi, %f (của float) là để thay thế một phao và %d (của số nguyên) là để thay thế một số nguyên%s (of string) is to replace a string, %f (of float) is to replace a float, and %d (of integer) is to replace an integer

# change this code
mystring = "hello"
myfloat = 10.0
myint = 20

# testing code
if mystring == "hello":
    print("String: %s" % mystring)
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
if isinstance(myint, int) and myint == 20:
    print("Integer: %d" % myint)

Đã trả lời ngày 28 tháng 1 năm 2019 lúc 2:32Jan 28, 2019 at 2:32

Hướng dẫn what is %d %s %f in python? - % d% s% f trong python là gì?

Chỉ để thêm vào @A. Câu trả lời của Gutierrez và đưa ra lời giải thích về lý do tại sao chúng tôi làm điều này; Trong các ví dụ trước (trên trang web được đề cập), chúng tôi được thông báo rằng Python "không hỗ trợ các nhà khai thác trộn giữa các số và chuỗi."Python "doesn't support mixing operators between numbers and strings."

Giải pháp này cho phép bạn vượt qua điều này.

Ví dụ, bạn sẽ nhận thấy rằng:

if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)

bản in

Float: 10.0

Nhưng sử dụng cú pháp "+ biến":

if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: " + myfloat)

Sẽ ném lỗi sau:

TypeError: Can't convert 'float' object to str implicitly

Đã trả lời ngày 7 tháng 8 năm 2021 lúc 21:28Aug 7, 2021 at 21:28

1

if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
3
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
4
2021 is here!!
4
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
6
2021 is here!!
6

Ví dụ 2:

& nbsp; Tuy nhiên, %D chỉ có thể được sử dụng cho các giá trị số. Mặt khác, một lỗi được trả về. & Nbsp;

Cách sử dụng %s trong Python

Toán tử %s được đặt trong đó chuỗi sẽ được chỉ định. Số lượng giá trị bạn muốn nối vào một chuỗi phải tương đương với số được chỉ định trong ngoặc đơn sau khi toán tử % ở cuối giá trị chuỗi. Mã sau đây minh họa việc sử dụng biểu tượng %s:

Python3

if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
0____11
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
2

if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
3
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
4
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
5
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
6
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
7

Đầu ra

Hey, Geek!

%D làm gì trong Python? & Nbsp;

Toán tử %D được sử dụng làm trình giữ chỗ để chỉ định các giá trị số nguyên, số thập phân hoặc số. Nó cho phép chúng tôi in các số trong chuỗi hoặc các giá trị khác. Toán tử %D được đặt trong đó số nguyên sẽ được chỉ định. Số điểm nổi được chuyển đổi tự động thành các giá trị thập phân. & NBSP;

Python3

if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
8
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
1
Float: 10.0
0

if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
3
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
4
Float: 10.0
3
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
6
Float: 10.0
5

Đầu ra

2021 is here!!

Cách sử dụng %D trong Python

Các số thập phân và hợp lý được làm tròn thành phần tích phân tuyệt đối và các số sau khi thập phân được loại bỏ, đó chỉ là các số nguyên được trích xuất. Mã sau đây minh họa việc sử dụng %D với số thập phân và phân số:

Python3

Float: 10.0
6
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
1
Float: 10.0
8
Float: 10.0
9
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: " + myfloat)
0

if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
3
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
4
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: " + myfloat)
3
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: " + myfloat)
4

if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
3
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
4
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: " + myfloat)
7
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
6
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: " + myfloat)
9

TypeError: Can't convert 'float' object to str implicitly
0
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
1
TypeError: Can't convert 'float' object to str implicitly
2

if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
3
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
4
TypeError: Can't convert 'float' object to str implicitly
5
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: " + myfloat)
4

if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
3
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
4
TypeError: Can't convert 'float' object to str implicitly
9
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
6
Hey, Geek!
1

Đầu ra

Rational number formatting using %d
2 is equal to 8/3 using this operator.
Decimal number formatting using %d
10 is equal to 10.9785 using this operator.

Cách sử dụng %D trong Python

Các số thập phân và hợp lý được làm tròn thành phần tích phân tuyệt đối và các số sau khi thập phân được loại bỏ, đó chỉ là các số nguyên được trích xuất. Mã sau đây minh họa việc sử dụng %D với số thập phân và phân số:

Float: 10.0
6
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
1
Float: 10.0
8
Float: 10.0
9
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: " + myfloat)
0

if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
3
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
4
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: " + myfloat)
7
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
6
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: " + myfloat)
9

TypeError: Can't convert 'float' object to str implicitly
0
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
1
TypeError: Can't convert 'float' object to str implicitly
2
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
3
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
4
TypeError: Can't convert 'float' object to str implicitly
9
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
6
Hey, Geek!
1
Sự khác biệt giữa %s và %d?Chúng ta có thể sử dụng kết hợp các toán tử cũng trong một chương trình duy nhất. Trước đó, hãy xóa một số khái niệm bằng cách so sánh như đã đưa ra dưới đây:
%s vận hành%d vận hành

Nó được sử dụng như một trình giữ chỗ cho các giá trị chuỗi. & Nbsp;

Nó được sử dụng như một trình giữ chỗ cho các giá trị số.

Sử dụng chuyển đổi chuỗi qua str () trước khi định dạng.

Python3

Sử dụng chuyển đổi thập phân thông qua int () trước khi định dạng.

%s cũng có thể chấp nhận các giá trị số và nó tự động thực hiện chuyển đổi loại. & nbsp;

if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
3
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
4
2021 is here!!
0
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: " + myfloat)
4

Trong trường hợp một chuỗi được chỉ định cho toán tử %D, một lỗi loại được trả về.

Đầu ra

Using %s 
Sita's age is 22.

Các lỗi phổ biến sử dụng %s và %d

Ví dụ 1:

Python3

Sử dụng chuyển đổi thập phân thông qua int () trước khi định dạng.

%s cũng có thể chấp nhận các giá trị số và nó tự động thực hiện chuyển đổi loại. & nbsp;

if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
3
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: %f" % myfloat)
4
Rational number formatting using %d
2 is equal to 8/3 using this operator.
Decimal number formatting using %d
10 is equal to 10.9785 using this operator.
5
if isinstance(myfloat, float) and myfloat == 10.0:
    print("Float: " + myfloat)
4

Trong trường hợp một chuỗi được chỉ định cho toán tử %D, một lỗi loại được trả về.

Các lỗi phổ biến sử dụng %s và %d

Using %d
Traceback (most recent call last):
 File "<string>", line 4, in <module>
TypeError: %d format: a number is required, not str

%D %s trong python là gì?

%s hoạt động một trình giữ chỗ cho một chuỗi trong khi %D hoạt động như một trình giữ chỗ cho một số.Các giá trị liên quan của chúng được truyền qua thông qua một tuple bằng toán tử %.. Their associated values are passed in via a tuple using the % operator.

%F trong in python là gì?

F có nghĩa là định dạng các chuỗi chữ và nó mới trong Python 3.6.Một chuỗi được định dạng theo nghĩa đen hoặc chuỗi F là một chuỗi theo nghĩa đen được đặt trước với 'f' hoặc 'f'.Các chuỗi này có thể chứa các trường thay thế, được phân tách bằng niềng răng xoăn {}.Formatted string literals and it's new in Python 3.6 . A formatted string literal or f-string is a string literal that is prefixed with 'f' or 'F' . These strings may contain replacement fields, which are expressions delimited by curly braces {} .

Làm thế nào để bạn sử dụng %s trong python?

Toán tử %s cho phép bạn thêm một giá trị vào chuỗi python.%S biểu thị rằng bạn muốn thêm một giá trị chuỗi vào một chuỗi.Toán tử % có thể được sử dụng với các cấu hình khác, chẳng hạn như % D, để định dạng các loại giá trị khác nhau.

Sự khác biệt giữa %s và %r trong Python là gì?

Khoa học dữ liệu thực tế bằng cách sử dụng Python, bộ xác định %s chuyển đổi đối tượng bằng cách sử dụng str () và %r chuyển đổi nó bằng cách sử dụng repr ().The %s specifier converts the object using str(), and %r converts it using repr().