首页 > 解决方案 > 如何在python中的json数据中传递字符串变量

问题描述

error - {"type":"error","message":"无效的手机号码。您必须输入少于 10 位数字或 API 中手机号码字段中有字母字符","code":"202 "}

import http.client
ph='9999999999'
conn = http.client.HTTPSConnection("api.msg91.com")
print(ph)
payload = '''{"sender": "SOCKET",
  "route": "4",
  "country": "91",
  "sms": [
    {
      "message": "Testing",
      "to": "'+str(ph)+'"
    }
  ]}'''

标签: python

解决方案


You can either use format or f-strings concept of Python. Below code snippet is an example of f-strings

var_name= "World"
payload = f"Hello {var_name}"

print(payload)
Hello World

For format:

var_name = "World"
payload = "Hello {var}".format(var=var_name)
print(payload)
Hello World

推荐阅读