Văn bản động Python

Định dạng các chuỗi để bao gồm các giá trị của các biến một cách mong muốn là một hoạt động chuỗi quan trọng, rất quan trọng đối với bất kỳ quy trình nào liên quan đến đầu ra chuỗi

Mỗi ngôn ngữ lập trình cung cấp cách định dạng chuỗi riêng. Ví dụ: hàm 

first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
4 của ngôn ngữ C [nghĩa đen là viết tắt của 'được định dạng in'] cho phép bạn chỉ định trình giữ chỗ cho số nguyên, số float, chuỗi, v.v. các giá trị trong chuỗi

Bạn chuyển các giá trị thực dưới dạng tham số bổ sung cho hàm
Tương tự, Python cung cấp phương thức 

first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
5 trên chuỗi, để chỉ định trình giữ chỗ cho các loại dữ liệu khác nhau và cung cấp giá trị của chúng làm đối số

Trong bài viết này, chúng ta sẽ hiểu động lực đằng sau phép nội suy chuỗi và xem xét các phương pháp khác nhau để nội suy chuỗi trong Python

 

Mục lục

1

 

Tại sao chúng ta cần nội suy chuỗi?

Ngôn ngữ lập trình có bản chất động. Hầu hết các ứng dụng trong thế giới thực, trong khi triển khai bất kỳ trường hợp sử dụng nào, sẽ tự động tính đến dữ liệu đến từ một trong nhiều nguồn như biến do người dùng xác định, đầu vào do người dùng cung cấp, dữ liệu tệp, biến hệ thống, v.v.

Những giá trị này không thể được mã hóa cứng vào chương trình
Bất cứ khi nào chúng ta xây dựng các chuỗi, chúng ta có thể cần phải cắm các giá trị đó vào các vị trí khác nhau trong chuỗi. Việc bổ sung dữ liệu này vào các chuỗi theo chương trình được gọi là phép nội suy chuỗi

Hãy tưởng tượng bạn được giao nhiệm vụ tạo bảng điều khiển dành cho sinh viên có trang chủ hiển thị các chi tiết cơ bản của sinh viên như tên, số thứ tự. , học kỳ, tên khóa học, v.v. và tóm tắt học tập như các môn đã học, kết quả thi, điểm học kỳ, điểm trung bình chung. vân vân
Bạn sẽ tự nhiên hiển thị thông tin này theo cách định dạng đẹp bằng cách sử dụng chuỗi. Và tất nhiên, bản tóm tắt này sẽ khác nhau đối với các sinh viên khác nhau tùy thuộc vào điểm dữ liệu cá nhân của họ
Một cách là in từng nhãn mô tả bản chất của thông tin, theo sau là giá trị tương ứng được tìm nạp từ cơ sở dữ liệu, sau đó lặp lại điều này tuần tự cho mọi thuộc tính

Nhưng điều này sẽ hạn chế đáng kể bản chất của việc định dạng thành một dạng rất đơn giản sẽ dễ bị lỗi hơn với số lượng thuộc tính mong muốn ngày càng tăng trong bản tóm tắt
Điều gì sẽ xảy ra nếu bạn chỉ có thể xác định toàn bộ mẫu tóm tắt dưới dạng một chuỗi duy nhất, với các phần giữ chỗ cho các giá trị của thuộc tính sinh viên tương ứng và sử dụng nó cho mọi sinh viên trong khi bổ sung các giá trị cho sinh viên mục tiêu?

Điều đó có làm cho cuộc sống của bạn với tư cách là một nhà phát triển dễ dàng hơn và làm cho kết quả của bạn ít bị lỗi hơn không?
Các phương thức nội suy chuỗi chỉ là lựa chọn phù hợp để bạn giải quyết loại vấn đề này

Tóm lại, nội suy chuỗi không chỉ cung cấp giá trị cho người dùng cuối mà còn nâng cao đáng kể trải nghiệm của nhà phát triển
Trải nghiệm nhà phát triển tốt sẽ bao gồm việc xây dựng các chuỗi động và phức tạp như vậy với một dòng mã tối thiểu
Python cung cấp nhiều phương thức nội suy chuỗi khác nhau, một số trong số đó hiệu quả hơn các phương thức khác trong khi một số dễ sử dụng hơn các phương thức khác

 

Nội suy bằng phép nối

Một trong những cách ngây thơ nhất để thực hiện phép nội suy chuỗi là nối các giá trị vào chuỗi bằng thao tác nối trên chuỗi
Nhưng điều này có nghĩa là lặp đi lặp lại việc nối chuỗi nhiều lần bằng số lượng biến mà chúng ta muốn cắm vào chuỗi.
Nó không chỉ làm tăng số lượng dòng mã mà còn làm tăng khả năng mắc lỗi khi xây dựng chuỗi.
Những vấn đề này sẽ chỉ trở nên tồi tệ hơn khi độ dài của chuỗi và số lượng biến được cắm vào tăng lên.

first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.9

output = "Welcome " + first_name + " " + last_name + ".\nPlease find below your personal and academic details:\n"+\
"Roll No. " + str[roll_no]+"\nCourse: "+course + " in " + major+"\n"+ "Semester: "+str[semester]+"\nCurrent CGPA: "+str[cgpa]+\
"\nIf you find any error in your details, please write to the administration at admin@harvard.edu"

print[output]

đầu ra

Như bạn có thể đã hình dung ra, phương pháp này liên quan đến quá nhiều thao tác nối
Mỗi khi bạn muốn chuyển đổi giữa các giá trị được lưu trữ trong biến và chuỗi ký tự thuần túy, bạn phải thực hiện nối chuỗi
Cũng lưu ý rằng để thêm các giá trị của các loại khác với chuỗi i. e số nguyên và số dấu phẩy động, bạn phải chuyển đổi chúng thành một chuỗi trước khi sử dụng chúng. Nếu bạn không làm điều đó, bạn sẽ gặp lỗi cho biết bạn không thể nối chuỗi với các loại khác
Như tôi đã chỉ ra trước đó, đây là một cách ngây thơ và làm tăng đáng kể phạm vi lỗi tiềm ẩn trong mã của bạn
Vì vậy, hãy xem xét một cách tốt hơn để thực hiện phép nội suy chuỗi trong Python i. e sử dụng toán tử modulo 

first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
6

 

Sử dụng toán tử modulo

Thông thường, toán tử modulo được sử dụng cho một phép toán số học. Nhưng trong Python, nó đã bị quá tải để thực hiện phép nội suy chuỗi
Bạn tạo một chuỗi ký tự với các trình giữ chỗ ở các vị trí khác nhau cho các giá trị của các loại dữ liệu khác nhau
Theo sau chuỗi ký tự này là toán tử modulo và một bộ các giá trị thực sẽ được thay thế ở các vị trí của trình giữ chỗ
Các trình giữ chỗ [hoặc cờ chuyển đổi] bên trong chuỗi ký tự cũng được chỉ định bằng cách sử dụng toán tử modulo
Mặc dù có thể sử dụng nhiều cờ chuyển đổi để chỉ định các loại giá trị khác nhau, nhưng hãy xem xét một số loại phổ biến hơn

Giá trị số nguyên có thể được chỉ định bằng cờ 'd' [

first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
7 để cụ thể hơn]
Số dấu phẩy động được chỉ định là 
first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
8 hoặc 
first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
9
Cũng có thể với một chuỗi khác bằng cách sử dụng cờ 
s = "Hi There! My name is Mr. %10s, I am %10d years old. I have %.2f dollars"%["Nasser", 17, 52.6789]
print[s]
0
Hãy sử dụng các cờ này trong ví dụ trước của chúng tôi

first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]

đầu ra

Bây giờ cái đó trông gọn gàng hơn nhiều. Bạn không phải chuyển đổi bất kỳ giá trị không phải chuỗi nào thành chuỗi và bạn chỉ phải xác định một chuỗi ký tự thay vì nhiều ký tự cho mỗi phần như trường hợp ghép nối

Nhưng lưu ý rằng giá trị CGPA, là một số dấu phẩy động, có rất nhiều số 0 ở cuối
Điều này là do theo mặc định, 

first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
8 sẽ thay thế các giá trị bằng độ chính xác 6

Ngay cả khi bạn đã chuyển một số nguyên thay cho cờ 

first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
8, nó vẫn sẽ thêm một dấu thập phân với 6 số 0 ở cuối
Điều này chắc chắn là không mong muốn và có thể khiến người dùng của bạn thất vọng

Tuy nhiên, có một giải pháp cho vấn đề này
Ngoài ra, bạn có thể chỉ định độ chính xác cho các giá trị dấu phẩy động hoặc dành một khoảng trống có độ dài cố định cho giá trị có số nguyên được chỉ định trước cờ chuyển đổi
Ví dụ: để chỉ định độ chính xác của 3 chữ số sau dấu thập phân, bạn có thể sử dụng cờ 

s = "Hi There! My name is Mr. %10s, I am %10d years old. I have %.2f dollars"%["Nasser", 17, 52.6789]
print[s]
3
Để dành khoảng trống 10 ký tự cho một giá trị số nguyên, bạn có thể sử dụng 
s = "Hi There! My name is Mr. %10s, I am %10d years old. I have %.2f dollars"%["Nasser", 17, 52.6789]
print[s]
4. Tương tự, đối với chuỗi, bạn có thể sử dụng 
s = "Hi There! My name is Mr. %10s, I am %10d years old. I have %.2f dollars"%["Nasser", 17, 52.6789]
print[s]
5
Nếu giá trị thực có độ dài nhỏ hơn độ dài dành riêng đã chỉ định [trong trường hợp này là 10], các khoảng trắng ở đầu sẽ được đưa vào theo độ dài còn lại

s = "Hi There! My name is Mr. %10s, I am %10d years old. I have %.2f dollars"%["Nasser", 17, 52.6789]
print[s]

đầu ra

Lưu ý khoảng trắng ở đầu có độ dài 4 cho chuỗi “Nasser”[length=6] và khoảng trắng có độ dài 8 cho 17 tuổi[length=2]
Ngoài ra, độ chính xác của giá trị thả nổi [52. 6789] hiện là 2 [với giá trị được làm tròn đến độ chính xác gần nhất]

Bạn cũng có thể dành khoảng trắng cho giá trị dấu phẩy động bằng cách nói, 

s = "Hi There! My name is Mr. %10s, I am %10d years old. I have %.2f dollars"%["Nasser", 17, 52.6789]
print[s]
6
Điều này sẽ cho biết độ dài là 10 và độ chính xác là 2
Ngoài ra, nếu bạn muốn khoảng trắng ở cuối thay vì khoảng trắng ở đầu mặc định, bạn có thể chỉ định độ dài bằng dấu âm
Nếu bạn chỉ có một giá trị để đưa vào chuỗi, bạn có thể bỏ qua bộ dữ liệu và chỉ định trực tiếp giá trị đó [ví dụ:.
s = "Hi There! My name is Mr. %10s, I am %10d years old. I have %.2f dollars"%["Nasser", 17, 52.6789]
print[s]
7]

Quan trọng nhất, Thứ tự của các giá trị được truyền trong bộ dữ liệu tương ứng với thứ tự của các cờ chuyển đổi trong chuỗi, vì vậy hãy luôn đảm bảo thứ tự của các đối số là chính xác
Ngoài ra, hãy đảm bảo rằng số lượng cờ chuyển đổi bằng với số lượng toán hạng bạn chuyển đến toán tử modulo

Bạn cũng có thể chỉ định các giá trị bằng cách sử dụng từ điển thay vì bộ dữ liệu, nơi bạn sẽ chèn khóa từ điển [được đặt trong ngoặc đơn] giữa modulo [

first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
6] và cờ chuyển đổi. Giá trị tương ứng của các khóa này sẽ được thay thế trong chuỗi tại các vị trí tương ứng
Một lợi thế của việc sử dụng từ điển thay vì bộ dữ liệu là bạn không phải lo lắng về thứ tự của các giá trị bên trong từ điển vì bản chất từ ​​điển là các ánh xạ không có thứ tự.

output = """Welcome %[first_name]s %[last_name]s.
Please find below your personal and academic details:
Roll No. %[roll_no]d
Course: %[course]s in %[major]s
Semester: %[semester]d
Current CGPA: %[cgpa].2f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%{"cgpa" : 8.934,
"first_name" : "Andrew",
"last_name" : "Tate",
"roll_no" : 61214000,
"course" : "MSc",
"major" : "Aerospace Engineering",
"semester" : 3}
print[output]

đầu ra

 

Sử dụng phương pháp định dạng

Python 3 đã giới thiệu một phương thức mới trên chuỗi để định dạng chuỗi. Đây là phương pháp 

s = "Hi There! My name is Mr. %10s, I am %10d years old. I have %.2f dollars"%["Nasser", 17, 52.6789]
print[s]
9
Nó làm cho phép nội suy chuỗi trở nên dễ dàng hơn nhiều và cũng khắc phục được một số vấn đề của phép toán modulo [chẳng hạn như hiển thị không chính xác các bộ dữ liệu và từ điển, v.v. ]
Phương thức 
first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
5 được sử dụng khá giống với toán tử modulo
Thay vì sử dụng cờ chuyển đổi trong chuỗi, bạn chỉ định dấu ngoặc nhọn. Bạn chuyển các giá trị thực làm đối số cho phương thức định dạng
Một trong những lợi thế rõ ràng và quan trọng nhất là bạn không cần phải nhớ các cờ chuyển đổi khác nhau cho từng loại giá trị
Tuy nhiên, bạn vẫn có thể tùy chọn chỉ định chúng nếu muốn, ví dụ:. để xác định độ chính xác

first_name = "Andrew"
last_name = "Tate"
roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome {} {}.
Please find below your personal and academic details:
Roll No. {}
Course: {:s} in {}
Semester: {}
Current CGPA: {:.2f}
If you find any error in your details, please write to the administration at admin@harvard.edu
""".format[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]

đầu ra

Lưu ý cách tôi có thể tùy chọn chỉ định cờ chuyển đổi cho khóa học và cũng chỉ định độ chính xác cho CGPA là số dấu phẩy động
Tuy nhiên, nếu bạn không chỉ định bất kỳ cờ chuyển đổi nào, thì theo mặc định, các giá trị được chuyển thành chuỗi. Vì vậy, không có độ chính xác mặc định là 6 cho các giá trị dấu phẩy động
Theo mặc định, thứ tự của các giá trị được thay thế giống với thứ tự của các đối số được truyền cho phương thức 

first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
5

Bạn có thể tùy chọn chỉ định chỉ mục của các đối số bên trong dấu ngoặc nhọn để vượt qua thứ tự mặc định này. Bạn cũng có thể chuyển các đối số đã đặt tên cho phương thức định dạng và sử dụng các tên bên trong dấu ngoặc nhọn tương ứng trong chuỗi

first_name = "Andrew"
last_name = "Tate"
roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome {1} {3}.
Please find below your personal and academic details:
Roll No. {4}
Course: {2:s} in {5}
Semester: {sem}
Current CGPA: {0:.2f}
If you find any error in your details, please write to the administration at admin@harvard.edu
""".format[cgpa, first_name, course, last_name,roll_no,  major, sem=semester ]
print[output]

đầu ra

Lưu ý cách tôi kết hợp một số chỉ số với các cờ chuyển đổi, được phân tách bằng dấu hai chấm [

output = """Welcome %[first_name]s %[last_name]s.
Please find below your personal and academic details:
Roll No. %[roll_no]d
Course: %[course]s in %[major]s
Semester: %[semester]d
Current CGPA: %[cgpa].2f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%{"cgpa" : 8.934,
"first_name" : "Andrew",
"last_name" : "Tate",
"roll_no" : 61214000,
"course" : "MSc",
"major" : "Aerospace Engineering",
"semester" : 3}
print[output]
2]
Ngoài ra, hãy lưu ý việc sử dụng đối số từ khóa 'sem' cho học kỳ và thông số kỹ thuật của nó trong dấu ngoặc nhọn tương ứng trong chuỗi

Một ưu điểm khác của việc sử dụng phương thức 

first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
5 là bạn không cần chỉ định số lượng đối số chính xác làm số lượng dấu ngoặc giữ chỗ nếu bạn sử dụng chỉ mục hoặc tham chiếu có tên cho các đối số
Hãy xem một ví dụ đơn giản chứng minh điều này

count = 8
s = "The guy's name is {name}. {name} loves fitness.\nHe runs {0} kilometers a day, eats {0} bananas and sleeps for {0} hours a day. {name} credits his love for fitness to his footballer father {1}.".format[
count, "Christiano Ronaldo", name = "Mateo"]
print[s]

đầu ra

Ở đây chúng tôi đã sử dụng 7 dấu ngoặc nhọn giữ chỗ nhưng chỉ truyền 3 đối số
Lưu ý rằng bạn không nên sử dụng kết hợp dấu ngoặc rỗng và đặc tả đối số/tên vì chúng sẽ dẫn đến lỗi
Không cần phải nói, bạn có thể chỉ định các giá trị cho trình giữ chỗ bằng cách sử dụng từ điển và chuyển chúng tới phương thức 

first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
5 bằng cách sử dụng toán tử sao đôi 
output = """Welcome %[first_name]s %[last_name]s.
Please find below your personal and academic details:
Roll No. %[roll_no]d
Course: %[course]s in %[major]s
Semester: %[semester]d
Current CGPA: %[cgpa].2f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%{"cgpa" : 8.934,
"first_name" : "Andrew",
"last_name" : "Tate",
"roll_no" : 61214000,
"course" : "MSc",
"major" : "Aerospace Engineering",
"semester" : 3}
print[output]
5 đến

Chúng tôi cũng có thể thêm các khoảng trắng ở đầu và cuối bằng cách sử dụng các toán tử 

output = """Welcome %[first_name]s %[last_name]s.
Please find below your personal and academic details:
Roll No. %[roll_no]d
Course: %[course]s in %[major]s
Semester: %[semester]d
Current CGPA: %[cgpa].2f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%{"cgpa" : 8.934,
"first_name" : "Andrew",
"last_name" : "Tate",
"roll_no" : 61214000,
"course" : "MSc",
"major" : "Aerospace Engineering",
"semester" : 3}
print[output]
6 và 
output = """Welcome %[first_name]s %[last_name]s.
Please find below your personal and academic details:
Roll No. %[roll_no]d
Course: %[course]s in %[major]s
Semester: %[semester]d
Current CGPA: %[cgpa].2f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%{"cgpa" : 8.934,
"first_name" : "Andrew",
"last_name" : "Tate",
"roll_no" : 61214000,
"course" : "MSc",
"major" : "Aerospace Engineering",
"semester" : 3}
print[output]
7 tương ứng với các cờ chuyển đổi tương ứng
Ngoài ra, bạn có thể thêm cả khoảng trắng ở đầu và ở cuối để căn giữa giá trị bằng cách sử dụng toán tử 
output = """Welcome %[first_name]s %[last_name]s.
Please find below your personal and academic details:
Roll No. %[roll_no]d
Course: %[course]s in %[major]s
Semester: %[semester]d
Current CGPA: %[cgpa].2f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%{"cgpa" : 8.934,
"first_name" : "Andrew",
"last_name" : "Tate",
"roll_no" : 61214000,
"course" : "MSc",
"major" : "Aerospace Engineering",
"semester" : 3}
print[output]
8
Bạn có thể tùy chọn chỉ định ký tự điền là một thứ khác với khoảng trắng

print["The king is back.His name is\n{:^25}\n{:^25}\n\n".format["Leo", "King Leo"]] 
print["Log dir:\nOpened file:{:15}\n{end_msg:*^30}".format["abc.txt","00:02h",end_msg="End of file"]]

đầu ra

Trong chuỗi đầu tiên, chúng tôi đã sử dụng căn giữa bằng toán tử 

output = """Welcome %[first_name]s %[last_name]s.
Please find below your personal and academic details:
Roll No. %[roll_no]d
Course: %[course]s in %[major]s
Semester: %[semester]d
Current CGPA: %[cgpa].2f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%{"cgpa" : 8.934,
"first_name" : "Andrew",
"last_name" : "Tate",
"roll_no" : 61214000,
"course" : "MSc",
"major" : "Aerospace Engineering",
"semester" : 3}
print[output]

Trong chuỗi thứ hai, chúng tôi đã thêm căn trái, phải và căn giữa. Trong trường hợp cuối cùng, chúng tôi cũng đã chỉ định ký tự điền là '*' thay vì khoảng trắng

Chúng ta có thể sử dụng dấu phẩy làm dấu tách hàng nghìn khi thay thế các số nguyên lớn bằng phương thức

first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
5
Đối với các đối tượng 
first_name = "Andrew"
last_name = "Tate"
roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome {} {}.
Please find below your personal and academic details:
Roll No. {}
Course: {:s} in {}
Semester: {}
Current CGPA: {:.2f}
If you find any error in your details, please write to the administration at admin@harvard.edu
""".format[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
1, chúng tôi có các định dạng bổ sung

large_no = 74937934973497
print[f"In America {large_no} would be written as {large_no:,}".format[
large_no=large_no
]]

from datetime import datetime
now = datetime.now[]
print["I am running this code on {dt:%d-%m-%Y} at {dt:%H:%M:%S}".format[dt = now]]

đầu ra

 

Chuỗi mẫu trong Python

Mô-đun 

first_name = "Andrew"
last_name = "Tate"
roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome {} {}.
Please find below your personal and academic details:
Roll No. {}
Course: {:s} in {}
Semester: {}
Current CGPA: {:.2f}
If you find any error in your details, please write to the administration at admin@harvard.edu
""".format[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
2 của Python cung cấp một lớp Mẫu được sử dụng để tạo một mẫu chuỗi với các trình giữ chỗ được đặt tên cho các giá trị

Chúng ta có thể thay thế các giá trị thực thay cho các trình giữ chỗ này bằng cách gọi phương thức 

first_name = "Andrew"
last_name = "Tate"
roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome {} {}.
Please find below your personal and academic details:
Roll No. {}
Course: {:s} in {}
Semester: {}
Current CGPA: {:.2f}
If you find any error in your details, please write to the administration at admin@harvard.edu
""".format[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
3 trên đối tượng mẫu và chuyển các giá trị cho từng trình giữ chỗ dưới dạng đối số từ khóa hoặc dưới dạng từ điển
Theo mặc định, trình giữ chỗ được xác định bằng dấu phân cách 
first_name = "Andrew"
last_name = "Tate"
roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome {} {}.
Please find below your personal and academic details:
Roll No. {}
Course: {:s} in {}
Semester: {}
Current CGPA: {:.2f}
If you find any error in your details, please write to the administration at admin@harvard.edu
""".format[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
4. Do đó, số nhận dạng trình giữ chỗ được chỉ định là 
first_name = "Andrew"
last_name = "Tate"
roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome {} {}.
Please find below your personal and academic details:
Roll No. {}
Course: {:s} in {}
Semester: {}
Current CGPA: {:.2f}
If you find any error in your details, please write to the administration at admin@harvard.edu
""".format[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
5 hoặc 
first_name = "Andrew"
last_name = "Tate"
roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome {} {}.
Please find below your personal and academic details:
Roll No. {}
Course: {:s} in {}
Semester: {}
Current CGPA: {:.2f}
If you find any error in your details, please write to the administration at admin@harvard.edu
""".format[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
6

from string import Template
    
template = CustomTemplate["""Welcome $fname $lname.
Please find below your personal and academic details:
Roll No. $roll_no
Course: $course in $major.
Semester: $sem
Current CGPA: $cgpa
If you find any error in your details, please write to the administration at admin@harvard.edu
"""]
first_name = "Andrew"
last_name = "Tate"
roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = template.substitute[fname = first_name,
                            lname = last_name,
                            roll_no = roll_no,
                            course = course,
                            major = major,
                            sem = semester,
                            cgpa = cgpa]
print[output]

đầu ra

Bạn cũng có thể chỉ định một dấu phân cách tùy chỉnh bằng cách xác định một lớp tùy chỉnh kế thừa lớp 

first_name = "Andrew"
last_name = "Tate"
roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome {} {}.
Please find below your personal and academic details:
Roll No. {}
Course: {:s} in {}
Semester: {}
Current CGPA: {:.2f}
If you find any error in your details, please write to the administration at admin@harvard.edu
""".format[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
7 và ghi đè thuộc tính 
first_name = "Andrew"
last_name = "Tate"
roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome {} {}.
Please find below your personal and academic details:
Roll No. {}
Course: {:s} in {}
Semester: {}
Current CGPA: {:.2f}
If you find any error in your details, please write to the administration at admin@harvard.edu
""".format[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
8
Một nhược điểm của việc sử dụng chuỗi mẫu để nội suy là bạn không thể sử dụng bộ chỉ định định dạng hoặc cờ chuyển đổi nếu bạn cần định dạng rất cụ thể hoặc độ chính xác nhất định cho các giá trị thập phân như trong phương thức 
first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
5 hoặc sử dụng toán tử modulo

 

Sử dụng chuỗi f

Trăn 3. 6 đã giới thiệu cho chúng tôi một cách nội suy chuỗi mới, thanh lịch hơn, ngắn gọn hơn và nhanh hơn được gọi là 'chuỗi f'
'f-strings' là viết tắt của 'Formatted String literals' và được cho là lựa chọn ưa thích nhất cho phép nội suy chuỗi ngày nay
chuỗi f cho phép chúng tôi đưa trực tiếp các biến, ký tự và biểu thức Python vào một chuỗi mà không cần gọi bất kỳ phương thức bổ sung nào hoặc sử dụng toán tử modulo
Chúng là các chuỗi bình thường nhưng có tiền tố 

first_name = "Andrew"
last_name = "Tate"
roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome {1} {3}.
Please find below your personal and academic details:
Roll No. {4}
Course: {2:s} in {5}
Semester: {sem}
Current CGPA: {0:.2f}
If you find any error in your details, please write to the administration at admin@harvard.edu
""".format[cgpa, first_name, course, last_name,roll_no,  major, sem=semester ]
print[output]
0 hoặc 
first_name = "Andrew"
last_name = "Tate"
roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome {1} {3}.
Please find below your personal and academic details:
Roll No. {4}
Course: {2:s} in {5}
Semester: {sem}
Current CGPA: {0:.2f}
If you find any error in your details, please write to the administration at admin@harvard.edu
""".format[cgpa, first_name, course, last_name,roll_no,  major, sem=semester ]
print[output]
1
Các giá trị được đưa vào được chỉ định trực tiếp bằng cách sử dụng dấu ngoặc nhọn 
first_name = "Andrew"
last_name = "Tate"
roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome {1} {3}.
Please find below your personal and academic details:
Roll No. {4}
Course: {2:s} in {5}
Semester: {sem}
Current CGPA: {0:.2f}
If you find any error in your details, please write to the administration at admin@harvard.edu
""".format[cgpa, first_name, course, last_name,roll_no,  major, sem=semester ]
print[output]
2
chuỗi f cũng cung cấp các tùy chọn để chỉ định loại và độ rộng của biểu thức đã truyền

Hãy lấy ví dụ của chúng ta về Mr. Tate và tân trang nó bằng chuỗi f

first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
0

đầu ra

Cho đến nay chúng tôi đã chuyển các biến. Nhưng các chuỗi f có thể được sử dụng để chỉ định theo nghĩa đen bất kỳ biểu thức Python nào trong chuỗi

first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
1

đầu ra

Lưu ý việc sử dụng các biểu thức số học, hằng số, phương thức và thông số kỹ thuật chính xác trong một chuỗi f đơn

Nếu bạn có một chuỗi f dài, bạn không muốn chỉ định tất cả trong một dòng mà trải dài trên nhiều dòng, bạn có thể chia chúng thành nhiều chuỗi f. Bạn có thể nhận thấy rằng trong tất cả các ví dụ của chúng tôi về Mr. Tate, chúng tôi đã chỉ định chuỗi dài của mình bằng ba dấu ngoặc kép 

first_name = "Andrew"
last_name = "Tate"
roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome {1} {3}.
Please find below your personal and academic details:
Roll No. {4}
Course: {2:s} in {5}
Semester: {sem}
Current CGPA: {0:.2f}
If you find any error in your details, please write to the administration at admin@harvard.edu
""".format[cgpa, first_name, course, last_name,roll_no,  major, sem=semester ]
print[output]
3
Dấu ngoặc kép định dạng chuỗi của bạn bằng cách thêm tất cả các khoảng trắng [dòng mới, tab, dấu cách] được đặt trong dấu ngoặc kép
Điều này có thể tạo xung đột với bất kỳ cách thụt đầu dòng mặc định nào do trình chỉnh sửa mã của bạn cung cấp và có thể tạo ra kết quả không mong muốn
Vậy thì làm cách nào để chúng ta tạo ra các chuỗi f nhiều dòng?
Bạn có thể đặt toàn bộ chuỗi bên trong dấu ngoặc đơn với mỗi chuỗi f trên một dòng mới hoặc bạn có thể bỏ qua dấu ngoặc đơn và mã hóa dòng mới bằng một ký tự thoát ở cuối mỗi chuỗi f
Ngoài ra, vì chúng tôi chỉ định các biểu thức bằng cách sử dụng dấu ngoặc nhọn, điều gì sẽ xảy ra nếu chúng tôi muốn đưa một dấu ngoặc nhọn thực sự vào chuỗi ký tự f của mình?
Bạn có thể thực hiện việc này bằng cách đặt dấu ngoặc nhọn bên trong dấu ngoặc nhọn 
first_name = "Andrew"
last_name = "Tate"
roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome {1} {3}.
Please find below your personal and academic details:
Roll No. {4}
Course: {2:s} in {5}
Semester: {sem}
Current CGPA: {0:.2f}
If you find any error in your details, please write to the administration at admin@harvard.edu
""".format[cgpa, first_name, course, last_name,roll_no,  major, sem=semester ]
print[output]
4 Điều gì sẽ xảy ra nếu chúng ta muốn có dấu ngoặc kép trong chuỗi f mà chính chuỗi này được chỉ định bằng dấu ngoặc kép?
Bạn có thể 'thoát' trích dẫn bằng cách sử dụng ký tự gạch chéo ngược '
first_name = "Andrew"
last_name = "Tate"
roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome {1} {3}.
Please find below your personal and academic details:
Roll No. {4}
Course: {2:s} in {5}
Semester: {sem}
Current CGPA: {0:.2f}
If you find any error in your details, please write to the administration at admin@harvard.edu
""".format[cgpa, first_name, course, last_name,roll_no,  major, sem=semester ]
print[output]
5'
Hãy sử dụng tất cả những ý tưởng này trong ví dụ đang chạy của chúng ta

first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
2

đầu ra

Chúng tôi đã chia một chuỗi f dài thành nhiều chuỗi bằng cách sử dụng dấu ngoặc đơn ở đầu ra1 và thoát khỏi return[enter] với 

first_name = "Andrew"
last_name = "Tate"
roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome {1} {3}.
Please find below your personal and academic details:
Roll No. {4}
Course: {2:s} in {5}
Semester: {sem}
Current CGPA: {0:.2f}
If you find any error in your details, please write to the administration at admin@harvard.edu
""".format[cgpa, first_name, course, last_name,roll_no,  major, sem=semester ]
print[output]
5 ở đầu ra2
Chúng tôi cũng đã thoát khỏi dấu ngoặc kép trong chuỗi f đầu tiên của output1 và bao gồm các dấu ngoặc nhọn thực tế trong chuỗi của chúng tôi trong chuỗi f đầu tiên của output2

 

Nội suy chuỗi trong JSON

Python cung cấp mô-đun 

first_name = "Andrew"
last_name = "Tate"
roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome {1} {3}.
Please find below your personal and academic details:
Roll No. {4}
Course: {2:s} in {5}
Semester: {sem}
Current CGPA: {0:.2f}
If you find any error in your details, please write to the administration at admin@harvard.edu
""".format[cgpa, first_name, course, last_name,roll_no,  major, sem=semester ]
print[output]
7 cung cấp các phương thức để chuyển đổi sang và từ các chuỗi JSON
JSON là định dạng được sử dụng rộng rãi để giao tiếp qua API

Chúng tôi có thể lưu trữ định dạng JSON mong muốn dưới dạng một chuỗi với các trình giữ chỗ bắt buộc và các giá trị thay thế một cách linh hoạt vào mẫu này trước khi gửi nó đi xa hơn
Chúng ta có thể đạt được điều này bằng cách sử dụng cả chuỗi f và phương thức 

first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
5, nhưng tôi khuyên bạn nên sử dụng phương thức định dạng vì các mẫu JSON dài có thể được lưu trữ trong tệp hoặc cơ sở dữ liệu chỉ có thể được đọc dưới dạng chuỗi Python bình thường thay vì chuỗi f

Hãy cẩn thận rằng các phương thức chuyển đổi chuỗi của JSON yêu cầu dữ liệu trong chuỗi phải ở định dạng JSON bao gồm dấu ngoặc nhọn

Vì vậy, bạn nên thoát khỏi những dấu ngoặc nhọn này. Chúng tôi đã thảo luận làm thế nào để làm điều này trong một phần trước

first_name = "Andrew"
last_name = "Tate"

roll_no = 61214000
course = "MSc"
major = "Aerospace Engineering"
semester = 3
cgpa = 8.934

output = """Welcome %s %s.
Please find below your personal and academic details:
Roll No. %d
Course: %s in %s
Semester: %d
Current CGPA: %f
If you find any error in your details, please write to the administration at admin@harvard.edu
"""%[first_name, last_name,roll_no, course, major, semester, cgpa]
print[output]
3

đầu ra

Tôi hy vọng bạn tìm thấy hướng dẫn hữu ích. Hãy quay lại nhé

  • Chia sẻ trên facebook
  • Tweet trên Twitter

Mokhtar Ebrahim

Mokhtar là người sáng lập LikeGeek. com. Anh ấy làm quản trị viên hệ thống Linux từ năm 2010. Ông chịu trách nhiệm duy trì, bảo mật và khắc phục sự cố máy chủ Linux cho nhiều khách hàng trên khắp thế giới. Anh ấy thích viết các tập lệnh shell và Python để tự động hóa công việc của mình

Chủ Đề