The Python dictionary str() method is used to retrieve a string representation of a dictionary.
In Python, a string is an immutable data type. A string is any text enclosed in double or single quote marks in Python, including alphabets, special characters, and numerals. In all programming languages, this data type is the most prevalent.
SyntaxFollowing is the syntax of Python dictionary str() method −
str(dict)Parameters
dict − This is the dictionary.
This method returns string representation of the dictionary.
ExampleThe following example shows the usage of Python dictionary str() method. Here a dictionary 'dict' is created. Then the string representation of the dictionary is retrieved using the str() method.
# Creating a dictionary dict = {'Name': 'Zara', 'Age': 7}; # Printing the result print ("Equivalent String : %s" % str (dict))
When we run above program, it produces following result −
Equivalent String : {'Name': 'Zara', 'Age': 7}Example
In here, we are creating an empty dictionary. Then the equivalent string representation of an empty dictionary is returned using str() method.
# Creating an empty dictionary dict_1 = {}; res = str(dict_1) # Printing the result print ("The equivalent string is: ", res)
Following is an output of the above code −
The equivalent string is: {}Example
In the following example we are creating the list of a nested dictionary. Then the equivalent string representation of the dictionary is returned using str() method.
dict_1 = [{'Universe' : {'Planet' : 'Earth'}}] print("The dictionary is: ",dict_1) # using str() method result = str(dict_1) print("The equivalent string is: ", result)
Output of the above code is given below −
The dictionary is: [{'Universe': {'Planet': 'Earth'}}] The equivalent string is: [{'Universe': {'Planet': 'Earth'}}]
python_dictionary.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4