如何连接和整数与字符串?

时间:2019-08-07 15:07:37

标签: string concatenation

当我尝试将weight_kg与字符串“公斤”连接在一起时,出现错误。我想要weight_kg的数字输出,后跟字符串“千克”。例如。 “ 50公斤”。

我尝试将weight_kg转换为字符串,以便将其链接为字符串“千克”

weight_lbs = input("Weight (lbs): ")
weight_kg = int(weight_lbs) * 0.45
print(weight_kg)

print(weight_kg) + str("kilograms")
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

2 个答案:

答案 0 :(得分:0)

  

print(weight_kg)+ str(“千克”)

您试图将print()(即NoneType)的返回值添加到string中。它们不能一起添加。

您可能想要这样的东西:

print( str(weight_kg) + "kilograms" )

首先将weight_kg转换为string,然后进行串联并打印出结果。

答案 1 :(得分:0)

尝试一下:

$urlcontent = $event['url']; 
$chcontent = curl_init();
$timeoutcontent = 5;
curl_setopt($chcontent, CURLOPT_URL, $urlcontent);
curl_setopt($chcontent, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($chcontent, CURLOPT_CONNECTTIMEOUT, $timeoutcontent);
curl_setopt($chcontent, CURLOPT_SSL_VERIFYPEER, false);
$htmlcontent = curl_exec($chcontent);
$infocontent = curl_getinfo($chcontent);
curl_close($chcontent);

@$domcontent->loadXML($htmlcontent);

foreach($domcontent->getElementsByTagName('span') as $spanon24content) {
    # Get url and title from <a> tags
    $innerHTMLspan = ''; 
    $childrenspan  = $spanon24content->childNodes;

    foreach ($childrenspan as $childspan) { 
        $innerHTMLspan .= $divspanon24content->ownerDocument->saveXML($childspan);
    }
}
$parsedcontent = $innerHTMLspan;

echo $parsedcontent;

无需再次将字符串转换为字符串, 并使用str(object)来将对象转换为str(就像您的情况下的整数) 并且打印函数语法为print(str(weight_kg)+"kilograms") 仅将print("things to be printed <must be inside the () of the print func>")连接2个字符串 因此,根据所有这些:

str3=str_1+str_2