Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3打印变量_Python_Variables_Printing - Fatal编程技术网

Python3打印变量

Python3打印变量,python,variables,printing,Python,Variables,Printing,在python3中打印变量集有更好的方法吗 在PHP中,我通常会这样做: echo“http://$username:$password@$server:$port” 但在Python3中,使用所有这些+,看起来非常难看,而且打字时间更长 print('http://'+username+':'+password+'@'+server+':'+port) python中是否有类似“$”的符号?谢谢。Python不支持字符串插值,但您可以使用字符串格式: 'http://{}:{}@{}:{}'.

在python3中打印变量集有更好的方法吗

在PHP中,我通常会这样做:
echo“http://$username:$password@$server:$port”

但在Python3中,使用所有这些+,看起来非常难看,而且打字时间更长

print('http://'+username+':'+password+'@'+server+':'+port)


python中是否有类似“$”的符号?谢谢。

Python不支持字符串插值,但您可以使用字符串格式:

'http://{}:{}@{}:{}'.format(username, password, server, port)
或者使用关键字参数(如果字典中已有参数,则最好使用):

您也可以稍微滥用
locals()
,但我不建议这样做:

'http://{username}:{password}@{server}:{port}'.format(**locals())

Python不支持字符串插值,但可以使用字符串格式:

'http://{}:{}@{}:{}'.format(username, password, server, port)
或者使用关键字参数(如果字典中已有参数,则最好使用):

您也可以稍微滥用
locals()
,但我不建议这样做:

'http://{username}:{password}@{server}:{port}'.format(**locals())

这对于一些代码来说要好得多:-)谢谢。但是你为什么不推荐本地人呢?这似乎是最干净的方式^ ^我很惊讶python没有在双引号中包含更好的方法来处理简单变量,这对某些代码来说要好得多:-)谢谢。但是你为什么不推荐本地人呢?这似乎是最干净的方式^ ^我很惊讶python没有在双引号中包含更好的简单变量方式