代码:
def solve_the_input(port):
port = hex(int(port))
split_result = port.split("0x")
split_port = split_result[1]
print 'input port is ',split_port
split_port.upper()
print 'input port is ',split_port
return split_port
if __name__ == "__main__":
if len(sys.argv) == 1:
print "please input a port"
else:
port = solve_the_input(sys.argv[1])
输入
python test.py 42328
实际输出:
input port is a558
input port is a558
预期输出:
input port is a558
input port is A558
我不知道为什么upper()方法没有按预期工作。
答案 0 :(得分:2)
upper方法以大写形式返回 new 字符串。所以使用
split_port = split_result[1].upper()
答案 1 :(得分:1)
几点意见
split_port.upper()
返回未分配回split_port
'0x'
。您可以改用replace
功能。会不那么复杂。具有替换功能的代码:
import sys
def solve_the_input(port):
port = hex(int(port))
result = port.replace("0x",'')
print 'input port is ',result
result = result.upper()
print 'input port is ',result
return result
if __name__ == "__main__":
if len(sys.argv) == 1:
print "please input a port"
else :
port = solve_the_input(sys.argv[1])
<强>输出:强>
C:\Users\dinesh_pundkar\Desktop>python c.py 1235
input port is 4d3
input port is 4D3
C:\Users\dinesh_pundkar\Desktop>
答案 2 :(得分:0)
Upper方法返回新字符串,但您需要存储该字符串。
split_port = split_result[1].upper()