Hướng dẫn how to assign values to strings in python - cách gán giá trị cho chuỗi trong python
\ (\ newcommand {l} [1] {\ | #1 \ |} \ newcommand {vl} [1] {\ l {\ vec Re} \, (#1)} \ newCommand {i} [1] {\ operatorName {im} \, (#1)} \) Show Tùy chọn 1 - Phương thức chuỗi formatBạn có thể sử dụng phương thức Chuỗi >>> shepherd = "Mary" >>> string_in_string = "Shepherd {} is on duty.".format(shepherd) >>> print(string_in_string) Shepherd Mary is on duty. Các niềng răng xoăn cho thấy giá trị chèn nên đi. Bạn có thể chèn nhiều hơn một giá trị. Các giá trị không phải là chuỗi, chúng có thể là số và các đối tượng Python khác. >>> shepherd = "Mary" >>> age = 32 >>> stuff_in_string = "Shepherd {} is {} years old.".format(shepherd, age) >>> print(stuff_in_string) Shepherd Mary is 32 years old. >>> 'Here is a {} floating point number'.format(3.33333) 'Here is a 3.33333 floating point number' Bạn có thể thực hiện định dạng phức tạp hơn của các số và chuỗi bằng cách sử dụng các tùy chọn định dạng trong dấu ngoặc xoăn - xem tài liệu về định dạng chuỗi giòn xoăn. Hệ thống này cho phép chúng tôi cung cấp các hướng dẫn định dạng cho những thứ như số, bằng cách sử dụng >>> shepherd = "Mary" >>> age = 32 >>> stuff_in_string = "Shepherd {} is {} years old.".format(shepherd, age) >>> print(stuff_in_string) Shepherd Mary is 32 years old.0) trong đó số lượng phải được chuẩn bị với >>> shepherd = "Mary" >>> age = 32 >>> stuff_in_string = "Shepherd {} is {} years old.".format(shepherd, age) >>> print(stuff_in_string) Shepherd Mary is 32 years old.1 để lấp đầy chiều rộng trường của >>> shepherd = "Mary" >>> age = 32 >>> stuff_in_string = "Shepherd {} is {} years old.".format(shepherd, age) >>> print(stuff_in_string) Shepherd Mary is 32 years old.2: >>> print("Number {:03d} is here.".format(11)) Number 011 is here. Điều này in một giá trị điểm nổi ( >>> shepherd = "Mary" >>> age = 32 >>> stuff_in_string = "Shepherd {} is {} years old.".format(shepherd, age) >>> print(stuff_in_string) Shepherd Mary is 32 years old.3) với chính xác >>> shepherd = "Mary" >>> age = 32 >>> stuff_in_string = "Shepherd {} is {} years old.".format(shepherd, age) >>> print(stuff_in_string) Shepherd Mary is 32 years old.4 chữ số sau điểm thập phân: >>> 'A formatted number - {:.4f}'.format(.2) 'A formatted number - 0.2000' Xem tài liệu định dạng chuỗi Python để biết thêm chi tiết và ví dụ. Tùy chọn 2 - F -String trong Python> = 3.6¶Nếu bạn có thể phụ thuộc vào việc có Python> = Phiên bản 3.6, thì bạn có một tùy chọn hấp dẫn khác, đó là sử dụng cú pháp theo nghĩa đen (F-String) được định dạng mới để chèn các giá trị biến. Một >>> shepherd = "Mary" >>> age = 32 >>> stuff_in_string = "Shepherd {} is {} years old.".format(shepherd, age) >>> print(stuff_in_string) Shepherd Mary is 32 years old.3 khi bắt đầu chuỗi cho Python cho phép bất kỳ tên biến hợp lệ hiện tại làm tên biến trong chuỗi. Ví dụ: đây là một ví dụ như ở trên, sử dụng cú pháp F-String: >>> shepherd = "Martha" >>> age = 34 >>> # Note f before first quote of string >>> stuff_in_string = f"Shepherd {shepherd} is {age} years old." >>> print(stuff_in_string) Shepherd Martha is 34 years old. Tùy chọn 3 - Định dạng trường học cũ % cũCó một phương thức định dạng chuỗi cũ hơn sử dụng toán tử >>> shepherd = "Mary" >>> age = 32 >>> stuff_in_string = "Shepherd {} is {} years old.".format(shepherd, age) >>> print(stuff_in_string) Shepherd Mary is 32 years old.6. Nó kém linh hoạt hơn một chút so với hai tùy chọn khác, nhưng bạn vẫn sẽ thấy nó được sử dụng trong mã cũ hơn và khi sử dụng định dạng >>> shepherd = "Mary" >>> age = 32 >>> stuff_in_string = "Shepherd {} is {} years old.".format(shepherd, age) >>> print(stuff_in_string) Shepherd Mary is 32 years old.6 là ngắn gọn hơn. Đối với toán tử >>> shepherd = "Mary" >>> age = 32 >>> stuff_in_string = "Shepherd {} is {} years old.".format(shepherd, age) >>> print(stuff_in_string) Shepherd Mary is 32 years old.6 hình thành, bạn hiển thị nơi các giá trị được chèn sẽ đi bằng ký tự >>> shepherd = "Mary" >>> age = 32 >>> stuff_in_string = "Shepherd {} is {} years old.".format(shepherd, age) >>> print(stuff_in_string) Shepherd Mary is 32 years old.6 theo sau là một định dạng định dạng, để nói cách chèn giá trị. Dưới đây là ví dụ trên, sử dụng định dạng >>> shepherd = "Mary" >>> age = 32 >>> stuff_in_string = "Shepherd {} is {} years old.".format(shepherd, age) >>> print(stuff_in_string) Shepherd Mary is 32 years old.6. Lưu ý điểm đánh dấu >>> 'Here is a {} floating point number'.format(3.33333) 'Here is a 3.33333 floating point number'1 để chèn một chuỗi và điểm đánh dấu >>> 'Here is a {} floating point number'.format(3.33333) 'Here is a 3.33333 floating point number'2 để chèn một số nguyên. >>> stuff_in_string = "Shepherd %s is %d years old." % (shepherd, age) >>> print(stuff_in_string) Shepherd Martha is 34 years old. |