有没有什么好办法来获取字符串的第二位数呢?例如:
aaa = 122
bbb = 333
rest = bbb-aaa
if rest[:2] == 1:
do something..发布于 2013-12-17 01:44:17
首先使用内置的str()函数将整数转换为字符串,然后对其进行相应的切片
所以试试这个:
if str(rest)[1] == '1':
#do something example:
print 'hi'发布于 2013-12-17 01:44:43
为其编制索引:
>>> mystr = "123"
>>> mystr[1]
'2'
>>> mystr[-2]
'2'
>>>如果它是一个数字,则需要首先使用str将其转换为字符串
>>> myint = 123
>>> str(myint)[1]
'2'
>>> str(myint)[-2]
'2'
>>>发布于 2021-09-28 03:35:29
def lastsecond(数字):
if(num>-9 and num<10):
return -1;
else:
temp=num/10;
rem=temp%10;
return int(rem);number=abs(int(input("enter number:“)print(”%d的最后一位数是%d"%(number,lastsecond(Number)
https://stackoverflow.com/questions/20617224
复制相似问题