str
có nghĩa là tạo ra một biểu diễn chuỗi của dữ liệu của đối tượng. Nếu bạn đang viết lớp của riêng mình và bạn muốn str
hoạt động cho bạn, hãy thêm:
def __str__[self]:
return "Some descriptive string"
print str[myObj]
sẽ gọi myObj.__str__[]
.
def __repr__[self]:
return "Some descriptive string"
0 là một phương pháp tương tự, thường tạo ra thông tin về thông tin lớp. Đối với hầu hết các đối tượng thư viện cốt lõi, def __repr__[self]:
return "Some descriptive string"
0 tạo ra tên lớp [và đôi khi một số thông tin lớp] giữa các khung góc. def __repr__[self]:
return "Some descriptive string"
0 sẽ được sử dụng, ví dụ, chỉ bằng cách gõ đối tượng của bạn vào ngăn tương tác của bạn, mà không cần sử dụng def __repr__[self]:
return "Some descriptive string"
3 hoặc bất cứ thứ gì khác.Bạn có thể xác định hành vi của
def __repr__[self]:
return "Some descriptive string"
0 cho các đối tượng của riêng bạn giống như bạn có thể xác định hành vi của str
:def __repr__[self]:
return "Some descriptive string"
def __repr__[self]:
return "Some descriptive string"
6 trong khung tương tác của bạn, hoặc def __repr__[self]:
return "Some descriptive string"
7, sẽ dẫn đến def __repr__[self]:
return "Some descriptive string"
8 Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luậnCreating strings in different ways :
Python xác định các chức năng chuyển đổi loại để chuyển đổi trực tiếp một loại dữ liệu sang loại dữ liệu khác hữu ích trong ngày và lập trình cạnh tranh. Một chuỗi là một chuỗi các ký tự. Chuỗi là một trong những loại phổ biến nhất trong Python. Chúng ta có thể tạo chúng chỉ đơn giản bằng cách đặt các ký tự trong trích dẫn.
def __repr__[self]:
return "Some descriptive string"
3Welcome to the Geeks for Geeks! Welcome Geek! Welcome again3
Ví dụ: Tạo chuỗi theo những cách khác nhau:
def __repr__[self]:
return "Some descriptive string"
3Welcome to the Geeks for Geeks! Welcome Geek! Welcome again8
Welcome to the Geeks for Geeks! Welcome Geek! Welcome again9
Welcome to the Geeks for Geeks! Welcome Geek! Welcome again0
def __repr__[self]:
return "Some descriptive string"
3str[built-in data type]2
9def __repr__[self]:
return "Some descriptive string"
Welcome to the Geeks for Geeks!
Welcome Geek!
Welcome again
0 Welcome to the Geeks for Geeks!
Welcome Geek!
Welcome again
1
Welcome to the Geeks for Geeks! Welcome Geek! Welcome again
Welcome to the Geeks for Geeks!
Welcome Geek!
Welcome again
4Welcome to the Geeks for Geeks!
Welcome Geek!
Welcome again
0 Welcome to the Geeks for Geeks!
Welcome Geek!
Welcome again
6
Đầu ra:
- Thay đổi bất kỳ loại dữ liệu nào thành một chuỗi
- Có hai cách để thay đổi bất kỳ loại dữ liệu nào thành một chuỗi trong Python:
Sử dụng chức năng str[built-in data type]
3Using the
str[built-in data type]5 function
Any built-in data type can be converted into its string representation by the
str[built-in data type]3 function. Built-in data type in python include:-
str[built-in data type]7,
str[built-in data type]8,
str[built-in data type]9,
Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :0,
Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :1,
Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :2etc.
Syntax :
str[built-in data type]
Thí dụ :
def __repr__[self]:
return "Some descriptive string"
33 def __repr__[self]:
return "Some descriptive string"
34def __repr__[self]:
return "Some descriptive string"
3Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :7
Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :8
Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :9
value of a =10 value of b =100
value of a =10 value of b =101
def __repr__[self]:
return "Some descriptive string"
35def __repr__[self]:
return "Some descriptive string"
36 def __repr__[self]:
return "Some descriptive string"
37def __repr__[self]:
return "Some descriptive string"
38def __repr__[self]:
return "Some descriptive string"
39def __repr__[self]:
return "Some descriptive string"
3Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :7
value of a =10 value of b =108
Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :9
value of a =10 value of b =100
str
1def __repr__[self]:
return "Some descriptive string"
40def __repr__[self]:
return "Some descriptive string"
38def __repr__[self]:
return "Some descriptive string"
42Welcome to the Geeks for Geeks! Welcome Geek! Welcome again0
Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :5
def __repr__[self]:
return "Some descriptive string"
3Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :7
str
7Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :9
value of a =10 value of b =100
str
0def __repr__[self]:
return "Some descriptive string"
40def __repr__[self]:
return "Some descriptive string"
38def __repr__[self]:
return "Some descriptive string"
47Welcome to the Geeks for Geeks! Welcome Geek! Welcome again0
Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :5
def __repr__[self]:
return "Some descriptive string"
3Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :7
value of a =10 value of b =108
Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :9
value of a =10 value of b =100
print str[myObj]
0print str[myObj]
1
Welcome to the Geeks for Geeks! Welcome Geek! Welcome again0
print str[myObj]
3print str[myObj]
4___def __repr__[self]:
return "Some descriptive string"
3Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :7
Welcome to the Geeks for Geeks! Welcome Geek! Welcome again2
myObj.__str__[]
6
Welcome to the Geeks for Geeks! Welcome Geek! Welcome again0
str
myObj.__str__[]
9def __repr__[self]:
return "Some descriptive string"
3Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :7
value of a =10 value of b =108
Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :9
value of a =10 value of b =100
def __repr__[self]:
return "Some descriptive string"
05def __repr__[self]:
return "Some descriptive string"
06Welcome to the Geeks for Geeks! Welcome Geek! Welcome again0
Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :7
print str[myObj]
4___def __repr__[self]:
return "Some descriptive string"
3Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :7
def __repr__[self]:
return "Some descriptive string"
17Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :9
value of a =10 value of b =100
def __repr__[self]:
return "Some descriptive string"
20def __repr__[self]:
return "Some descriptive string"
21Welcome to the Geeks for Geeks! Welcome Geek! Welcome again0
str
def __repr__[self]:
return "Some descriptive string"
24def __repr__[self]:
return "Some descriptive string"
3Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :7
def __repr__[self]:
return "Some descriptive string"
27Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :9
value of a =10 value of b =100
def __repr__[self]:
return "Some descriptive string"
30Output:
Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :
Phương pháp 2: Xác định hàm str[built-in data type]
4 cho lớp do người dùng xác định được chuyển đổi thành biểu diễn chuỗi. Đối với một lớp do người dùng xác định được chuyển đổi thành biểu diễn chuỗi, hàm str[built-in data type]
4 cần được xác định trong nó.Defining
str[built-in data type]4 function for a user defined class to be converted to string representation. For a user defined class to be converted to string representation,
str[built-in data type]4 function needs to be defined in it.
Thí dụ :
def __repr__[self]:
return "Some descriptive string"
33 def __repr__[self]:
return "Some descriptive string"
34def __repr__[self]:
return "Some descriptive string"
35def __repr__[self]:
return "Some descriptive string"
36 def __repr__[self]:
return "Some descriptive string"
37def __repr__[self]:
return "Some descriptive string"
38def __repr__[self]:
return "Some descriptive string"
39def __repr__[self]:
return "Some descriptive string"
40def __repr__[self]:
return "Some descriptive string"
38def __repr__[self]:
return "Some descriptive string"
42Welcome to the Geeks for Geeks! Welcome Geek! Welcome again0
Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :5
def __repr__[self]:
return "Some descriptive string"
40def __repr__[self]:
return "Some descriptive string"
38def __repr__[self]:
return "Some descriptive string"
47Welcome to the Geeks for Geeks! Welcome Geek! Welcome again0
Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :5
def __repr__[self]:
return "Some descriptive string"
35def __repr__[self]:
return "Some descriptive string"
36 def __repr__[self]:
return "Some descriptive string"
52def __repr__[self]:
return "Some descriptive string"
38def __repr__[self]:
return "Some descriptive string"
39def __repr__[self]:
return "Some descriptive string"
40def __repr__[self]:
return "Some descriptive string"
56 def __repr__[self]:
return "Some descriptive string"
57def __repr__[self]:
return "Some descriptive string"
58def __repr__[self]:
return "Some descriptive string"
59Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :7
def __repr__[self]:
return "Some descriptive string"
38def __repr__[self]:
return "Some descriptive string"
62def __repr__[self]:
return "Some descriptive string"
38def __repr__[self]:
return "Some descriptive string"
64def __repr__[self]:
return "Some descriptive string"
65Welcome to the Geeks for Geeks! Welcome Geek! Welcome again0
def __repr__[self]:
return "Some descriptive string"
67def __repr__[self]:
return "Some descriptive string"
3Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :7
str
def __repr__[self]:
return "Some descriptive string"
71def __repr__[self]:
return "Some descriptive string"
3Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :7
value of a =10 value of b =100
Type before : Type after : Type before : Type after : Type before : Type after : Type before : Type after :7
str
def __repr__[self]:
return "Some descriptive string"
77Output:
value of a =10 value of b =10