python - And and Or confusion -
i have file i'm parsing, on each line in same column 1 of values; 0, 1 or -1. want parse numeric values in line below. trying via if statement and/or operators. cannot head around and/or when test print statement, output either prints 1's or prints 0's.
desired output: 0 1 -1 if number.startswith('1' , '0'): return number.append(' ') print number
'1' , '0'
resolves '0'
:
>>> '1' , '0' '0'
boolean operators not same thing english grammatical constructs!
the str.startswith()
method takes tuple of values test for:
number.startswith(('1', '0'))
and it'll return true
if string starts '1'
or starts '0'
.
however, in case can use string formatting pad out number:
return format(int(number), '-2d')
this pads number out field 2 characters wide, leaving space negative sign:\
>>> format(-1, '-2d') '-1' >>> format(0, '-2d') ' 0' >>> format(1, '-2d') ' 1'
Comments
Post a Comment