Python | Konverter ordbogsobjekt til streng
Ordbogen er en vigtig beholder og bruges næsten i alle koder til daglig programmering samt webudvikling med Python . Jo mere det bruges, jo mere er kravet om at mestre det, og det er derfor nødvendigt at lære om dem.
Input: { 'testname' : 'akshat','test2name' : 'manjeet','test3name' : 'nikhil'} Output: {'testname': 'akshat', 'test2name': 'manjeet', 'test3name': 'nikhil'} Explanation: Input type is but the output type is Lad os se de forskellige måder at ændre en ordbog til en streng.
Ordbogsobjekt til streng Samtale
Nedenfor er de metoder, vi vil dække i denne artikel:
- Ved brug af json.dumps() metode
- Ved brug af str() fungere
- Ved brug af printmetode
Konvertering af Dict til String i Python ved hjælp af metoden json.dumps().
Her kan vi bruge dump() metoden fra JSON bibliotek ved at importere det, som konverterer ordbogsdatatypen til streng. I nedenstående kode tager vi først en ordbogstest1, derefter bruger vi json.dumps metode og bestå tes1-ordbogen i den, og vi vil få det nødvendige resultat i snor format.
Python3
import> json> # initialising dictionary> test1> => {> 'testname'> :> 'akshat'> ,> > 'test2name'> :> 'manjeet'> ,> > 'test3name'> :> 'nikhil'> }> # print original dictionary> print> (> type> (test1))> print> (> 'initial dictionary = '> , test1)> # convert dictionary into string> result> => json.dumps(test1)> # printing result as string> print> (> '
'> ,> type> (result))> print> (> 'final string = '> , result)> |
Produktion:
initial dictionary = {‘testname’: ‘akshat’, ‘test2name’: ‘manjeet’, ‘test3name’: ‘nikhil’} final string = {testname: akshat, test2name: manjeet, test3name: nikhil} Rumkompleksitet: På)
Tidskompleksitet: På)
Ordbog til strengkonvertering ved hjælp af str() funktion
Det str() funktion konverterer den angivne værdi til en streng. Strengefunktionen er også nyttig til at konvertere datatypen til strengtype ved at vi overfører ordbogen til denne metode, og den vil konvertere datatypeformularens ordbog til strengdatatype.
Python3
test1> => {> 'testname'> :> 'akshat'> ,> > 'test2name'> :> 'manjeet'> ,> > 'test3name'> :> 'nikhil'> }> # print original dictionary> print> (> type> (test1))> print> (> 'initial dictionary = '> , test1)> # convert dictionary into string> result> => str> (test1)> # print resulting string> print> (> '
'> ,> type> (result))> print> (> 'final string = '> , result)> |
Produktion:
initial dictionary = {‘test2name’: ‘manjeet’, ‘testname’: ‘akshat’, ‘test3name’: ‘nikhil’} final string = {‘test2name’: ‘manjeet’, ‘testname’: ‘akshat’, ‘test3name’: ‘nikhil’} Rumkompleksitet: På)
Tidskompleksitet: På)
Konverter ordbog til streng ved hjælp af printmetode
En anden tilgang til at konvertere et ordbogsobjekt til en streng er at bruge printet. Udskriften giver en måde at smuk-udskrive vilkårligt Python datastrukturer i en form, der Print kan bruges som input til tolken.
Her er et eksempel på brug af print-modulet til at konvertere et ordbogsobjekt til en streng:
Python3
import> pprint> # Initialize dictionary> d> => {> 'testname'> :> 'akshat'> ,> 'test2name'> :> 'manjeet'> ,> 'test3name'> :> 'nikhil'> }> # Print original dictionary> print> (f> 'Original dictionary: {d}'> )> # Convert dictionary into string using pprint.pformat()> result> => pprint.pformat(d)> # Print resulting string> print> (f> '
Resulting string: {result}'> )> print> (> 'Type is: '> ,> type> (result))> |
Produktion
Original dictionary: {'testname': 'akshat', 'test2name': 'manjeet', 'test3name': 'nikhil'} Resulting string: {'test2name': 'manjeet', 'test3name': 'nikhil', 'testname': 'akshat'} Type is: Space complexity : O(n) Time complexity : O(n) The print module provides more control over the formatting of the resulting string, such as indentation and line width, than the built-in str and json.dumps functions.