Python str() Function Explained

The str() function in Python is used to convert an object into a string. It can take different types of data like numbers, lists, tuples, dictionaries, etc. as input and convert them into their respective string forms. For example:

num = 10
str_num = str(num)
print(str_num)  # 输出:'10'

list1 = [1, 2, 3]
str_list = str(list1)
print(str_list)  # 输出:'[1, 2, 3]'

dict1 = {'a': 1, 'b': 2}
str_dict = str(dict1)
print(str_dict)  # 输出:{'a': 1, 'b': 2}'

Please note that for custom class objects, you need to define the __str__ method within the class in order to convert it to a string representation.

bannerAds