python输出不带引号的字符串,如何返回不带引号的字符串Python 3

I've got to write a single-input module that can convert decimals to Bukiyip (some ancient language with a counting base of 3 or 4). For the purpose of the assignment, we only need to work with base 3.

I've written some code that does this, but it returns my Bukiyip number with quotes, leaving me with an answer such as '110' for 12.

Please help me understand how to work around this? I'm new to Python and keen to learn so explanations will be really appreciated.

def bukiyip_to_decimal(num):

convert_to_string = "012"

if num < 3:

return convert_to_string[num]

else:

return bukiyip_to_decimal(num//3) + convert_to_string[num%3]

I've also tried the following, but get errors.

else:

result = bukiyip_to_decimal(num//3) + convert_to_string[num%3]

print(int(result))

解决方案

You are either echoing the return value in your interpreter, including the result in a container (such as a list, dictionary, set or tuple), or directly producing the repr() output for your result.

Your function (rightly) returns a string. When echoing in the interpreter or using the repr() function you are given a debugging-friendly representation, which for strings means Python will format the value in a way you can copy and paste right back into Python to reproduce the value. That means that the quotes are included.

Just print the value itself:

>>> result = bukiyip_to_decimal(12)

>>> result

'110'

>>> print(result)

110

or use it in other output:

>>> print('The Bukiyip representation for 12 is {}'.format(result))

The Bukiyip representation for 12 is 110

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值