2018年1月11日 星期四

Python: Use double quotes to display strings

Python: Use double quotes to display strings

Python Basic: Exercise-130 with Solution

Write a Python program to use double quotes to display strings.
Sample Solution:-
import json
print(json.dumps({'Alex': 1, 'Suresh': 2, 'Agnessa': 3}))
========= RESTART: F:/Python_APSC/py-ex-basic-130.py ==========
{"Alex": 1, "Agnessa": 3, "Suresh": 2}
>>> 

Python JSON

 JSON is JavaScript Object Notation.
import json
# initialize different data
str_data = 'normal string'
int_data = 1
float_data = 1.50
list_data = [str_data, int_data, float_data]
nested_list = [int_data, float_data, list_data]
dictionary = {
    'int': int_data,
    'str': str_data,
    'float': float_data,
    'list': list_data,
    'nested list': nested_list
}

# convert them to JSON data and then print it
print('String :', json.dumps(str_data))
print('Integer :', json.dumps(int_data))
print('Float :', json.dumps(float_data))
print('List :', json.dumps(list_data))
print('Nested List :', json.dumps(nested_list, indent=2))
print('Dictionary :', json.dumps(dictionary, indent=2))  # the json data will be indented

========= RESTART: F:/Python_APSC/py-ex-basic-130-1.py ============
String : "normal string"
Integer : 1
Float : 1.5
List : ["normal string", 1, 1.5]
Nested List : [
  1,
  1.5,
  [
    "normal string",
    1,
    1.5
  ]
]
Dictionary : {
  "str": "normal string",
  "float": 1.5,
  "list": [
    "normal string",
    1,
    1.5
  ],
  "int": 1,
  "nested list": [
    1,
    1.5,
    [
      "normal string",
      1,
      1.5
    ]
  ]
}
>>> 


沒有留言:

張貼留言

MQTT Explorer 與 Node-Red 介面的實驗

 MQTT Explorer 與 Node-Red 介面的實驗 MQTT EXplorer 與 Node-Red介面的設定 (1) 設定  MQTT EXplorer Client    (2)        Node-Red LED ON  --> MQTT Explor...